|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
As you learned on the previous page, thetrystatement defines the scope of its associated exception handlers. You associate exception handlers with atrystatement by providing one or morecatchblocks directly after thetryblock:There can be no intervening code between the end of thetry { . . . } catch ( . . . ) { . . . } catch ( . . . ) { . . . } . . .trystatement and the beginning of the firstcatchstatement. The general form of Java'scatchstatement is:As you can see, thecatch (SomeThrowableObject variableName) { Java statements }catchstatement requires a single formal argument. The argument to thecatchstatement looks like an argument declaration for a method. The argument type, SomeThrowableObject, declares the type of exception that the handler can handle and must be the name of a class that inherits from theThrowableclass defined in the
java.langpackage. When Java programs throw an exception they are really just throwing an object, and only objects that derive from
Throwablecan be thrown. You'll learn more about throwing exceptions in How to Throw Exceptions.variableName is the name by which the handler can refer to the exception caught by the handler. For example, the exception handlers for the
writeListmethod (shown later) each call the exception'sgetMessagemethod using the exception's declared namee:You access the instance variables and methods of exceptions in the same manner that you access the instance variables and methods of other objects.e.getMessage()getMessageis a method provided by theThrowableclass that prints additional information about the error that occurred. TheThrowableclass also implements two methods for filling in and printing the contents of the execution stack when the exception occurred. Subclasses ofThrowablecan add other methods or instance variables. To find out what methods an exception implements, check its class definition and definitions for any of its ancestor classes.The
catchblock contains a series of legal Java statements. These statements are executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose type matches that of the exception thrown.The
writeListmethod from theListOfNumbersclass uses two exception handlers for its
trystatement, with one handler for each of the two types of exceptions that can be thrown within thetryblock --ArrayIndexOutOfBoundsExceptionandIOException.try { . . . } catch (ArrayIndexOutOfBoundsException e) { System.err.println("Caught ArrayIndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }
The two exception handlers used by thewriteListmethod are very specialized. Each handles only one type of exception. The Java language allows you to write general exception handlers that handle multiple types of exceptions.As you know, Java exceptions are
Throwableobjects; they are instances ofThrowableor a subclass ofThrowable. The Java packages contain numerous classes that derive fromThrowableand thus, build a hierarchy ofThrowableclasses.Your exception handler can be written to handle any class that inherits from Throwable. If you write a handler for a "leaf" class (a class with no subclasses), you've written a specialized handler: it will only handle exceptions 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 any exception whose type is the node class or any of its subclasses.Let's modify the
writeListmethod once again. Only this time, let's write it so that it handles bothIOExceptionsandArrayIndexOutOfBoundsExceptions. The closest common ancester ofIOExceptionandArrayIndexOutOfBoundsExceptionis theExceptionclass. An exception handler that handles both types of exceptions looks like this:Thetry { . . . } catch (Exception e) { System.err.println("Exception caught: " + e.getMessage()); }Exceptionclass is pretty high in theThrowableclass hierarchy. So in addition to theIOExceptionandArrayIndexOutOfBoundsExceptiontypes that this exception handler is intended to catch, it will catch numerous other types. Generally speaking, your exception handlers should be more specialized. Handlers that can catch most or all exceptions are typically useless for error recovery because the handler has to determine what type of exception occurred anyway to determine the best recovery strategy. Also, exception handlers that are too general can make code more error prone by catching and handling exceptions that weren't anticipated by the programmer and for which the handler was not intended.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.