November 23, 2014
Hot Topics:

Chained Exceptions in Java

  • July 23, 2002
  • By Richard G. Baldwin
  • Send Email »
  • More Articles »

Define four constructors in new exception classes

If you define a new Throwable class and wish for an object of that class to have a cause associated with it, you should define constructors that take a Throwable as a parameter and pass that Throwable up the inheritance hierarchy to one of the Throwable constructors that takes a Throwable as a parameter.  (Your new class should have one constructor that matches the formal argument list of each of the four constructors in the Throwable class).  You will see an example of this in the sample program.

Sample program named Excep20

Now it's time to take a look at some code.  A complete listing of the program named Excep20 is shown in Listing 7 near the end of the lesson.  I will discuss this program in fragments.

Two new exception classes

This program defines two new classes, named NewEx01 and NewEx02, which are used to instantiate and throw exception objects.  The code in each of these classes is essentially the same, so I will show and discuss only one of them.  The class named NewEx01 is shown in Listing 1.

class NewEx01 extends Exception{
  public NewEx01() {
  }//end constructor

  public NewEx01(String message){
  }//end constructor

  public NewEx01(Throwable throwable){
  }//end constructor

  public NewEx01(String message,
                  Throwable throwable){
    super(message, throwable);
  }//end constructor
}//end NewEx01

Listing 1

A checked exception class

As you can see from Listing 1, the class extends Exception.  As a result, it is a checked exception class.  (Of course, it is also a Throwable class, because Exception extends Throwable.).

Four overloaded constructors

As suggested in an earlier paragraph, the code in the class definition consists of four overloaded constructors, each corresponding to one of the four overloaded constructors in the Throwable class.

Each constructor (other than the one that takes no arguments) uses the super keyword to pass the incoming parameters up to the constructor for the superclass.  This makes it possible to invoke the methods of the Throwable class on an object instantiated from this class, and have those methods behave correctly.

Summary of the program's behavior

Basically, this program consists of the controlling named Excep20, two exception classes named NewEx01 and NewEx02, and another class named Class01.

The class named Class01 defines three methods named meth01, meth02, and meth03.  The main method of the controlling class instantiates a new object of the class Class01, and invokes the method named meth01 on that object's reference.

The method named meth01 invokes the method named meth02, and the method named meth02 invokes the method named meth03.

Oops!  meth03 throws an exception

The code in meth03 attempts to perform an integer divide by zero, causing an ArithmeticException to be thrown by the runtime system.  This code is executed inside a try block.  A catch block that follows the try block catches the ArithmeticException.

Instantiate and throw a different exception with a cause

Code in the catch block instantiates a new object of type IndexOutOfBoundsException and invokes the initCause method on that object, causing it to encapsulate a reference to the ArithmeticException that was caught by the catch block.  Then it throws the IndexOutOfBoundsException object, which is caught by the method named meth02 (the next method up the call stack).

Instantiate and throw a different exception with a cause - again

The method named meth02 catches that object and encapsulates it in an object of type NewEx02, which it throws.

This exception is caught by meth01, which performs a similar action, and throws an object of type NewEx01, which is caught by the main method.

Print information at each stop along the way

At each stop along the way, the method catching the exception object displays certain information about the object that it caught, including the message and the cause encapsulated in that object.

Print the stack trace

Finally, the main method prints a stack trace showing the complete history of the call stack since the ArithmeticException was thrown way down in the method named meth03.

Code from the inside out

I am going to discuss the code from the inside out.  Listing 3 shows the method named meth03, where the ArithmeticException gets thrown as a result of an attempt to perform an integer divide by zero.

  void meth03(){

      int x = 3/0;
    }//end try block

    catch(ArithmeticException e){
      IndexOutOfBoundsException ex = 
         new IndexOutOfBoundsException(
                   "Msg from metho03");
      throw ex;
    }//end catch block

  }//end meth03

Listing 2

Why throw an IndexOutOfBoundsException object?

You may be wondering why I chose to have this method throw an IndexOutOfBoundsException object, since this program has absolutely nothing to do with an index.

I wanted to demonstrate the use of the initCause method to encapsulate the reference to the ArithmeticException object (as the cause) in an object instantiated from a class that predates release 1.4.  As near as I can tell, the IndexOutOfBoundsException class has been part of Java for as least as far back as release 1.1.

The meth03 method implements try/catch exception handling

As you can see in Listing 2, the code in meth03 consists of a try block followed by a catch block.  The code in the try block attempts to perform an integer divide by zero, causing an ArithmeticException to be thrown.

This exception is caught in the catch block, where the incoming reference to the ArithmeticException object is known locally by the name e

Instantiate a new IndexOutOfBoundsException object

The code in the catch block instantiates a new object of the class IndexOutOfBoundsException, saving the object's reference in the reference variable named ex.

Encapsulate the cause

Then the code in the catch block invokes the initCause method on that reference variable, passing the ArithmeticException object's reference as a parameter.  The initCause method is new to JDK 1.4.  In this case, it causes the ArithmeticException object's reference to be encapsulated in the IndexOutOfBoundsException object as a cause.

As we will see shortly, the getCause method can later be invoked on the reference to that object to get access to the original ArithmeticException object.

Page 2 of 4

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel