CHAPTER 19

EXCEPTION HANDLING

An unusual situation may occur sometimes while running the programs. There might be errors and some of them may be fatal. Take a simple case of divide by zero. Older languages give some error message and halt the execution of the program. If the errors are fatal, one cannot do anything about it but if they are non-fatal, the language should allow us to do some damage control exercise. Java provides elegant way of exception handling precisely for this purpose.

19.1 Why Use Exception Handling

One of the most important reasons for handling exception is that when a program terminates abruptly many undesirable things happen. Certain resources, like open files and the World Wide Web (WWW) connection, remain blocked. The operating system (OS) does not get them back. If your program has done any important work or calculations, the efforts are lost. When exception occurs, the program stops running. We cannot do anything about it afterwards. But if we handle exceptions properly, we can do some damage control exercise. We can give the allocated resources back to the OS and save some results. In lighter vain, we can send SOS to our friends.

Let us start our study with a simple example.

Problem: Write a program to show divide by zero exception.

Solution: See Program 19.1.

 

PROGRAM 19.1 Divide by zero exception

 //   ex2.java

 class ex2

 { public static void main (String args [])

   { int num = 20;

     int den = 0 ;

     int result;

     result = num / den ;

     System.out.println(“--->ex2.java starts<---“);

     System.out.println(“result is “ + result);

   }

 }

If you run this program, you will get the following output.

 

Output

 Exception in thread main

 java.lang.ArithmeticException: / by zero

         void ex2.main(java.lang.String[])

The program aborts if there is exception. This is ArithmeticException. The great thing about it is that Java is highly object oriented and converts exceptions into objects. Java also provides some nice ways to handle these exceptions (objects). Let us study it in detail.

19.2 Types of Exceptions

There are two types of exceptions. The first one represents error. These are hard situations and lead to abrupt termination of the program. Also, these are unrecoverable exceptions. A table describing various types of error is given in Section 19.8. Exceptions of second type are not so hard. They can be handled properly. Here program is not required to terminate immediately. Of course, we are required to code using rules of the game.

Package java.lang defines a class Throwable. This class has two sub-classes namely Error and Exception. Errors or exception occurring in the programs are objects of these classes.

Figure 19.1 Illustrates hierarchy of exception classes.

Hierarchy of Exceptions

Figure 19.1 Hierarchy of Exceptions

19.3 Checked and Unchecked Exceptions

Exceptions can be divided in two groups, runtime exceptions and non-runtime exception. Runtime exceptions occur within the Java runtime system. The examples are the following:

  • arithmetic exceptions, such as dividing by zero,
  • pointer exceptions, such as trying to access an object’s members through a null reference, and
  • indexing exceptions, such as trying to access an array element with an index that is too large or too small.

Java has defined a class RuntimeException which acts as super-class for all individual runtime error classes. When we write methods, we need not catch or specify these exceptions. Hence, they can be termed as “unchecked exceptions”. The name is somewhat misleading as the JVM does check for these exceptions.

The other exceptions are those occurring in our code. Consider a case of exceptions that occur during input/output. The compiler ensures that we either catch or specify them. That is why they are called “checked exceptions”. It is possible that we may forget putting “try block” around critical code, which is likely to produce say IOException. The compiler will refuse to compile the code. It will give the following error message.

unreported exception java.io.IOException; must be caught or declared to be thrown

If we add words throws Exception to any method, it will compile without giving any error message.

19.4 Exception Handling Constructs

Java offers an elegant solution to exception handling. It provides two constructs, try and catch. A try construct consists of keyword try followed by block. (Block is a group of statements enclosed in curly brackets.) We will term this block as try block.

The code which is likely to produce exception is kept in this block. This is followed by catch construct. It consists of keyword catch followed exception parameter (in round brackets) followed by a block. We will term this block as catch block. When executing code from try block raises an exception, the control is handed over to catch block. After this block ends, the control goes to next sentence in the program. Please note that the control does not go to try block again (see Program 19.2).

Problem: Write a program to illustrate try–catch action.

Solution: See Program 19.2.

 

PROGRAM 19.2 Try–Catch demo

 //      ex21.java

 class ex21

 {  public static void main (String args [])

    {  int num = 20;

       int den = 0 ;

       int result=0;

       System.out.println(“--->ex21.java starts<---“);

       try {

               result = num / den ;

               System.out.println(“Just for test”);

           }

       catch(Exception e)

          {  System.out.println(“Exception is caught”);

          }

       System.out.println(“result is “ + result);

    }

 }

If you run this program, you will get the following output.

 

Output

 --->ex21.java starts<---

 Exception is caught

 result is 0

All the aforementioned points can be verified from the output. Note the absence of string

Just for test

in the output.

These actions are shown pictorially in Figure 19.2.

try–catch mechanism

Figure 19.2 try–catch mechanism

Let us see a small program.

Problem: Write a program to demonstrate NullPointerException.

Solution: See Program 19.3.

 

PROGRAM 19.3 NullPointerException

 //      ex31.java

 class ex31

 {  public static void main (String args [])

    {  int j =0;

       String str ="welcome" ;

       System.out.println("<---ex31.java--->");

       try {

             str = null;

             j = str.length();

             System.out.println(j);

          }

       catch( NullPointerException e)

          {  System.out.println("Exception is caught");

             System.out.println(e.toString());

          }

       System.out.println("last line of the program");

    }

 }

If you run this program, you will get the following output.

 

Output

 <---ex31.java--->

 Exception is caught

 java.lang.NullPointerException

 last line of the program

One more program in Section 19.8 illustrates the NumberFormatException.

Warning!

Remember the proverb “a stitch in time saves nine”. Some programmers may bypass the mechanism of checked exception. The compiler will not issue a warning. But such shortcuts may prove disastrous in the long range. When you write a method which has many statements throwing different exceptions, the code fails to compile. You get the following message.

“unreported exception java.io.IOException; must be caught or
declared to be thrown”.

There is a tendency to add throws Exception. The program compiles without any objection. But the program may fail when exception is thrown and we will have no chance to recover. In a professional application, this will not be acceptable.

19.5 Keyword finally

As an extension to try and catch, Java offers another facility in terms of keyword finally. If we place a block labelled as finally, just after the catch block, it will always be executed. Let us see its action in a program.

Problem: Write a program to illustrate keyword finally.

Solution: See Program 19.4.

 

PROGRAM 19.4 Keyword finally

   //     ex23.java

 class ex23

 {  public static void main (String args [])

    {  int num = 20;

       int den = 0 ;

       int result=0;

       System.out.println(“--->ex23.java starts<---”);

       try {

               result = num / den ;

               System.out.println(“Just for test”);

           }

       catch(Exception e)

          {  System.out.println(“Exception is caught”);

          }

       finally

          {   System.out.println(

                 “All resources may be released here”);

          }

       System.out.println(“result is “ + result);

   }

 }

If you run this program, you will get the following output.

 

Output

 --->ex23.java starts<---

 Exception is caught

 All resources may be released here

 result is 0

Note that the catch block is executed followed by finally block. Afterwards, the control goes to next statement after finally. Hence, the line “result is 0” appears in the output.

In the aforementioned program, if we change the denominator to 20, there will be no exception. As stated earlier, finally block will still be executed. You can see the program ex23a.java in Section 19.8.. You will notice that finally block is executed when catch block is not.

19.6 Throw Statement

So far we have only seen exception objects being generated because of some peculiarity in our code. Java allows us to define our own exceptions. It is called user-defined exception. For this, we must define a sub-class of Exception. The throw statement is used to throw the object of type exception.

Let us take a simple program from banking. One wants to withdraw money from an account. Naturally, for the operation to succeed, balance has to be greater than withdrawal. If someone tries to withdraw more than the balance there should be exception. Let us name it as insufficient balance.

See the following program.

Problem: Write a program to show that a new exception class can be defined.

Solution: Let us take a simple program from banking. We will define a class IBException (IB = Insufficient Balance). See Program 19.5.

 

PROGRAM 19.5 Insufficient Balance Exception

 //      ex5a.java

 class  IBException extends Exception

 {  public  String toString()

      { return ("Transaction denied " +"Insufficient balance");

      }

 } ;

 class ex5a

 { public static void main (String args []) throws IBException

   {  int balance = 20;

      int withdraw = 30 ;

      int result;

      System.out.println("<---ex5a.java starts--->");

      try

       { result = balance - withdraw ;

         if (result < 0) throw new IBException() ;

         balance = result;

         System.out.println("balance is " + balance);

       }

      catch (IBException e)

        { System.out.println(e.toString());

        }

   }

 }

If you compile this program, you will get the following output.

 

Output

  <---ex5a.java starts--->

 Transaction denied

 Insufficient balance

Well the action on exception was very simple in this case. In real-life situation, action that is more appropriate will be required. If it is an ATM rather than a bank, an alarm in watchman’s cabin may be sounded.

19.7 Multiple Catch Blocks

Java allows multiple catch blocks after a try bloc. Any given code is likely to raise different possible exceptions. The action in response cannot be easily unified. For clarity of code, we should use different catch blocks for each exception.

When an exception occurs, the system tries to match with the type of exception one by one. When it finds a suitably matching catch block, system executes this block. Any further catch blocks are ignored.

Let us study this by a suitable example.

Problem: Write a program to demonstrate multiple catch blocks.

Solution: See Program 19.6.

 

PROGRAM 19.6 Multiple Catch Blocks I

 //     ex43.java

 class ex43

 { public static void main (String args [])

  { int A[] ={0,1,2};

    int j,k = 0 ;

    String str ="welcome" ;

    System.out.println("<---ex43.java--->");

    try {

         str = null; // note 1

         j = str.length();

         System.out.println("string length : "+ j) ;

         k=5 ;

         A[k]=0;

         System.out.println(A[k]);

         }

    catch( NullPointerException e)

         { System.out.println("Exception is caught");

           System.out.println(e.toString());

         }

    catch(ArrayIndexOutOfBoundsException e)

          {  System.out.println("Exception is caught");

             System.out.println(e.toString());

          }

   }

 }

If you run this program, you will get the following output.

 

Output

 <---ex43.java--->

 Exception is caught

 java.lang.NullPointerException

If you comment out line marked note 1, you will get the following output.

 

Output

  <---ex43.java--->

 string length : 7

 Exception is caught

 java.lang.ArrayIndexOutOfBoundsException: 5

When we write multiple catch blocks, the exception classes should be independent. If not, they should be properly structured. Let us study the following program before discussing the same.

Problem: Write a program to demonstrate unstructured multiple catch blocks.

Solution: See Program 19.7.

 

PROGRAM 19.7 Multiple Catch Blocks II

 //      ex42.java

 class ex42

 {  public static void main (String args [])

    {  int A[] ={0,1,2};

       int k = 0 ;

       System.out.println("<---ex42.java--->");

       try {  k=5 ;

              A[k]=0;

              System.out.println(A[k]);

           }

       catch(Exception e)

          {

          }

       catch(ArrayIndexOutOfBoundsException e)

          {  System.out.println("Exception is caught");

             System.out.println(e.toString());

          }

       System.out.println("last line of the program");

    }

 }

If you run this program, you will get the following compilation error.

 

Output

 Compiling javac

 ex42.java ex42.java:14: exception java.lang.ArrayIndexOutOfBoundsException has already

 bee

 n caught

         catch(ArrayIndexOutOfBoundsException e)

         ^

 1 error

 press any key to continue

Please note the following points:

ArrayIndexOutOfBoundsException is a derivative (sub-subclass) of class Exception. We have stated a rule that exceptions are matched sequentially. Though Exception matches ArrayIndexOutOfBoundsException, such an exception clause will never get executed because exception is caught by earlier catch block. Hence, Java flags it as compilation error.

It is possible for Java developers to ignore such a case, but in certain circumstances, it can mar the real and correct action. This will reduce the quality of a program and also introduce bugs. Hence, this strictness on the part of Java should be appreciated.

After catch block, control flows out to the statement after try block. We have not coded any action here. You will appreciate the example if you consider an ATM in place of a bank counter.

19.8 Collection of End of Chapter Programs

19.8.1 Clause finally

Problem: Write a program to show that clause finally is executed even when there is no exception.

Solution: See Program 19.8.

 

PROGRAM 19.8 Clause finally

 //      ex23a.java

 class ex23a

 {  public static void main (String args [])

    {  int num = 20;

       int den = 20 ;

       int result=0;

       System.out.println("--->ex23a.java starts<---");

       try {

               result = num / den ;

               System.out.println("Just for test");

           }

       catch(Exception e)

          {  System.out.println("Exception is caught");

          }

       finally

         {   System.out.println(

               "All resources may be released here");

         }

       System.out.println("result is " + result);

    }

 }

If you run this program, you will get the following output.

 

Output

 --->ex23a.java starts<---

 Just for test

 All resources may be released here

 result is 1

19.8.2 Number format exception

Earlier, in chapter on Wrapper classes, we studied a method parseByte(String str) which returns the byte equivalent of number contained in the string specified by str. It assumes radix 10. This throws NumberFormatException. The reason is that not every string can conforms to a number.

Problem: Write a program to demonstrate NumberFormatException.

Solution: See Program 19.9.

 

PROGRAM 19.9 NumberFormatException I

 //      ex35.java

 class ex35

 {  public static void main (String args [])

      {  byte num ;

         System.out.println("<---ex35.java--->");

         num = Byte.parseByte("XY");

         System.out.println("num is " + num);

      }

 }

If you run this program, you will get the following error output.

 

Output

 <---ex35.java--->

 Exception in thread "main" java.lang.NumberFormatException:

 For input string: "XY"

         at java.lang.NumberFormatException.forInputString(Unknown Source)

         at java.lang.Integer.parseInt(Unknown Source)

         at java.lang.Byte.parseByte(Unknown Source)

         at java.lang.Byte.parseByte(Unknown Source)

         at ex35.main(ex35.java:6)

Please note that string XY does not represent a number in radix 10. Therefore, there is an exception. Had the string been 23, you would have got the following output.

   <---ex35a.java--->

   num is 23

In the aforementioned program, we have not caught the exception. It is known that parseByte() is likely to throw the exception. Hence, a better idea is to use a catch statement which writes a suitable comment like

   ”parameter is not a valid byte value.”

on the screen.

Java has large numbers of exceptions. They are organized in deep hierarchy. It is not possible to list all of them here. We have selected few common exceptions for ready reference. Readers are advised to refer to API specifications for complete details.

Table 19.1 lists the important sub-classes of class Exception.

 

Table 19.1 Selected Sub-Classes of Class Exception

Exception sub-classes Description
AWTException Signals AWT exception
ClassNotFoundException Occurs when an application fails to find the class to load
DataFormatException Thrown when data format error occurs
FontFormatException Thrown by create font method
InterruptedException Occurs when thread is interrupted
IOException Input/output exception
NoSuchFieldException Occurs when a class does not have a specified field
NoSuchMethodException Occurs when a class does not have a specified method
PrinterException Indicates exceptional conditions in print system
RuntimeException This is a super-class of the exceptions that gets thrown during normal JVM Operation
UnsupportedAudioFileException Occurs when audio file does not conform to specified format

Table 19.2 describes the various kinds of checked and unchecked exceptions supported in Java. Please note that we have selected only few common exceptions.

 

Table 19.2 Summary of Checked and Unchecked Exceptions

Exception Description
Part I
The following exceptions are derived from class RuntimeException. Therefore, they are unchecked exceptions.
ArithmeticException Thrown when an exceptional arithmetic condition has occurred
ArrayIndexOutOfBoundsException Thrown to indicate that an array has been accessed with an illegal index
ArrayStoreException Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects
ClassNotFoundException Thrown when an application tries to load in a class through its string name using The forName method in class Class
IllegalArgumentException Thrown to indicate that a method has been passed an illegal or inappropriate argument
IllegalMonitorStateException Thrown to indicate that a thread has attempted to wait on an object’s monitor or to notify other threads waiting on an object’s monitor, without owning the specified monitor
IllegalStateException Signals that a method has been invoked at an illegal or inappropriate time
IllegalThreadStateException Thrown to indicate that a thread is not in an appropriate state for the requested operation
IndexOutOfBoundsException Thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range
NegativeArraySizeException Thrown if an application tries to create an array with negative size
NullPointerException Thrown when an application attempts to use null in a case where an object is required
NumberFormatException Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have an appropriate format
RuntimeException RuntimeException is the super-class of those exceptions that can be thrown during the normal operation of the JVM
SecurityException Thrown by the security manager to indicate a security violation
StringIndexOutOfBoundsException Thrown by the charAt method in class String and by other string methods to indicate that an index is either negative or greater than or equal to the size of the string
UnsupportedOperationException Thrown to indicate that the requested operation is not supported
Part II
The following exceptions are not the sub-classes of class RuntimeException. Therefore, they are checked exceptions.
ClassCastException Thrown to indicate that the code has attempted to cast an object to a sub-class, of which it is not an instance.
InstantiationException Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.
InterruptedException Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.
NoSuchFieldException Signals that the class does not have a field of a specified name
NoSuchMethodException Thrown when a particular method cannot be found
CloneNotSupportedException Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object’s class does not implement the Cloneable interface
IllegalAccessException Thrown when an application tries to load in a class, but the currently executing method does not have an access to the definition of the specified class because the class is not public and in another package
EOFException Thrown when an end of file or end of stream has been reached unexpectedly during input Thrown when attempt to open the specified file fails

 

Table 19.3 describes the various kinds of errors supported in Java.

 

Table 19.3 Summary of Errors

AbstractMethodError Thrown when an application tries to call an abstract method
ClassCircularityError Thrown when circularity has been detected while initializing a class
ClassFormatError Thrown when the JVM attempts to read a class file and determines that the file is malformed or otherwise cannot be interpreted as a class file
Error An error is a sub-class of Throwable that indicates serious problems which a reasonable application should not try to catch
ExceptionInInitializerError Signals that an unexpected exception has occurred in a static initializer
IllegalAccessError Thrown if an application attempts to access or modify a field, or to call a method that it does not have an access to
IncompatibleClassChangeError Thrown when an incompatible class change has occurred to some class definition
InstantiationError Thrown when an application tries to use the Java new construct to instantiate an abstract class or an interface
InternalError Thrown to indicate that some unexpected internal error has occurred in the JVM
LinkageError Sub-classes of LinkageError indicate that a class has some dependency on another class; however, the latter class has its incompatibly changed after the compilation of the former class
NoClassDefFoundError Thrown if the JVM or a classloader tries to load in the definition of a class (as part of a normal method call or as a part of creating a new instance using the new expression) and no definition of the class is found
NoSuchFieldError Thrown if an application tries to access or modify a specified field of an object, and the object no longer has that field
NoSuchMethodError Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method
OutOfMemoryError Thrown when the JVM cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector
StackOverflowError Thrown when a stack overflow occurs because an application recurses too deeply
ThreadDeath An instance of ThreadDeath is thrown in the victim thread when the stop method with zero arguments in class Thread is called
UnknownError Thrown when an unknown but serious exception has occurred in the JVM
UnsatisfiedLinkError Thrown if the JVM cannot find an appropriate native-language definition of a method declared native
UnsupportedClassVersionError Thrown when the JVM attempts to read a class file and determines that the major and minor version numbers in the file are not supported
VerifyError Thrown when the “verifier” detects that a class file, though well formed, contains some sort of internal inconsistency or security problem
VirtualMachineError Thrown to indicate that the JVM is broken or has run out of resources necessary for it to continue operating

Keywords

ArithmeticException, ArrayIndexOutOfBoundsException, catch block, checked exceptions, class Throwable, divide by zero exception, Exception, finally, NullPointerException, NumberFormatException, throw statement, throws, try block, unchecked exceptions, user-defined exception

RuleBook

try A try block must be followed by a catch block. Catch block need not be present if finally block is present
finally When a finally block is present it is always executed irrespective of occurrence of exception
finally A finally block is optional
Exception class The root class of all the exception classes is the Exception class
Instantiationexception Instantiation exception means an attempt to create an abstract class or interface
Stack overflow Stack overflow is an example of Runtime exception
Runtime exception Exception An exception is an abnormal condition that disrupts normal program flow
throw A throw statement causes an exception to be thrown
throws A special clause called throws is used in method “definition” to indicate that a method may possibly throw an exception checked Exceptions Only runtime exceptions are checked exceptions, all others are unchecked exceptions.

C++ Corner

It may be noted that finally keyword (and the facility thereof) is not present in C++.

Review Questions

  1. What do you understand by the term checked exception?
  2. What do you understand by the term unchecked exception?
  3. Does the control go back to try block, after catch block is executed?
  4. What is the advantage of clause finally?
  5. What is the difference between keywords throw and throws.
  6. List ten different exceptions defined in Java.
  7. List any five errors defined in Java.
  8. What is the difference between a runtime error and a compile time error? Explain using a suitable example.
  9. Write short note on exception handling in Java.
  10. Distinguish between error and exception.
  11. What are the advantages of exception handling?
  12. Explain multiple catch clauses in exception handling.

Exercises

  1. Write a program to demonstrate IndexOutOfBoundsException.
  2. Write a program to demonstrate NullPointerException.
  3. Write a program to demonstrate StringIndexOutOfBoundsException.
  4. State whether the following program will compile. If yes, describe the output. If no, give reasons.

    **********************************************************

    //      ex37.java

    class ex37

    {  public static void main (String args [])

         {  byte num = 20;

            System.out.println("<---ex37.java starts--->");

          try {

                num = Byte.parseByte("128");

              }

          catch(Exception e) {

                  System.out.println("Exception is caught");

                  System.out.println(e);

          };

          System.out.println("num is " + num);

    **********************************************************

         }

    }

  5. State whether the following program will compile. If yes, describe the output. If no, give reasons.

    **********************************************************

    //  ex52.java

    class ex52

    {

      public static void main (String args [ ])

       {

         try

          {

            double x = 0.0;

            throw (new Exception ("Thrown")) ;

            //return ; Note 1

          }

         catch (Exception e)

          {

            System.out.println("Exception caught");

            return ;

          }

         finally

            {  System.out.println("***finally***"); }

       }

    }

    **********************************************************

  6. State whether the following program will compile. If yes, describe the output. If no, give reasons.

    **********************************************************

    //      ex3a.java

    class  E extends Exception

      { } ;

    class ex3a

    {  public static void main (String args [])

         {  int balance = 20;

            int withdraw = 10 ;

            int result;

            System.out.println(“--->ex3a.java starts<---“);

            result = balance - withdraw ;

            if (result < 0) throw new E() ;

            balance = result;

            System.out.println(“balance is “ + balance);

         }

    }

    **********************************************************

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.12.11