Exception Chaining


One of the new features in J2SE 1.4 is exception chaining . It is common to catch one exception only to throw a different one. For example, you might catch a NumberFormatException caused by incorrect user input. In that case, you might want to throw your custom exception (for example, SCJDException ) with a message about the origin. You can also include the original exception in the one you throw. Listing 17.5 is an adaptation of Listing 17.4, but it demonstrates the use of exception chaining, in which one exception class is nested within another as the exception climbs the call stack.

Listing 17.5 Using Exception Chaining
 /* Also, this example demonstrates exception chaining, in which  * one exception class is nested within another as the  * exception climbs the call stack.  */ import java.io.IOException; public class MyExceptionHandler2 extends IOException {     static int MAIN_METHOD = 1;     static int MY_METHOD = 2;     static int MY_NEXT_METHOD = 3;     static int CATCH = MY_METHOD; /*  * main() gets an exception that comes from deep within the  * call stack, in this case from myNextMethod().  * @param args command line arguements  */     public static void main(String[] args)     {         try         {              myMethod();         } catch (IOException ioe)         {              System.out.println("main() - " + ioe);         } catch (Exception e)         {              System.out.println("main() - " + e);         } finally         {              System.out.println("main() - finally");         }     }     public static void myMethod() throws Exception     {         if(CATCH == MY_METHOD)         {             throw new Exception("myMethod() - ",                   new SCJDException("Oops!"));         } else if(CATCH == MY_NEXT_METHOD)         {             myNextMethod();         }     }     public static void myNextMethod() throws Exception     {         if(CATCH == MY_NEXT_METHOD)         {             throw new Exception("myNextMethod() - ",                   new SCJDException("Oops!"));         }     } } 

Depending on which setting you assign to the CATCH flag, Listing 17.5 produces the following report about exceptions (e.g., CATCH = MY_METHOD ):

 java.lang.Exception: myNextMethod() -     at MyExceptionHandler.myNextMethod(MyExceptionHandler.java:40)     at MyExceptionHandler.myMethod(MyExceptionHandler.java:31)     at MyExceptionHandler.main(MyExceptionHandler.java:13) Caused by: SCJDException: Oops!     ... 3 more main() - finally 
graphics/tip_icon.gif

If you use a custom exception class, use exception chaining in your solution to make the resulting error message clearer. Also, using exception chaining demonstrates a thorough understanding of exception-handling techniques.




JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
JavaT 2 Developer Exam CramT 2 (Exam CX-310-252A and CX-310-027)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 187

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net