<< Chapter < Page Chapter >> Page >

"Your exception handler can be written to handle any class that inheritsfrom Throwable . If you write a handler for a "leaf" class (a class with no subclasses), you've written a specialized handler: it will only handleexceptions of that specific type. If you write a handler for a "node" class (a class with subclasses), you've written a general handler: it will handle anyexception whose type is the node class or any of its subclasses."

You have a choice

Therefore, when writing exception handlers, you have a choice. You can write a handler whose exception type corresponds to a node in the inheritancehierarchy, and it will be appropriate to catch exceptions of that type, or any subclass of that type.

Alternately, you can write a handler whose exception type corresponds to a leaf, in which case, it will be appropriate to catch exceptions of that type only.

And finally, you can mix and match, writing some exception handlers whose type corresponds to a node, and other exception handlers whose type correspondsto a leaf. In all cases, however, be sure to position your exception handlers in reverse subclass order, with the furthermost subclass from the root appearingfirst, and the root class appearing last.

The finally block

And finally (no pun intended), Campione and Walrath tell us:

"Java's finally block provides a mechanism that allows your method to clean up after itself regardless of what happens within the try block. Use thefinally block to close files or release other system resources."

To elaborate, the finally block can be used to provide a mechanism for cleaning up open files, etc., before allowing control to be passed to adifferent part of the program. You accomplish this by writing the cleanup code within a finally block.

Code in finally block is always executed

It is important to remember that the runtime system always executes the code within the finally block regardless of what happens within the try block.

If no exceptions are thrown, none of the code in catch blocks is executed, but the code in the finally block is executed.

If an exception is thrown and the code in an exception handler is executed, once the execution of that code is complete, control is passed to the finally block and the code in the finally block is executed.

(There is one important exception to the above. If the code in the catch block terminates the program by executing System.exit(0) , the code in the finally block will not be executed.)

The power of the finally block

The sample program shown in Listing 5 illustrates the power of the finally block.

Listing 5 . The power of the finally block.
/*File Excep15.java Copyright 2002, R. G. BaldwinTested with JDK 1.4.0 under Win2000 **************************************/class Excep15{ public static void main(String[] args){new Excep15().aMethod(); }//end main//---------------------------------// void aMethod(){try{ int x = 5/0;}//end try block catch(ArithmeticException e){System.out.println( "In catch, terminating aMethod");return; }//end catch blockfinally{ System.out.println("Executing finally block"); }//end finally blockSystem.out.println( "Out of catch block");}//end aMethod }//end class Excep15

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask