Rethrowing an Exception

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 9.  Exception Handling


When you catch an exception, you do not necessarily have to resolve the problem: You can catch the exception, examine it, and then rethrow it. This is accomplished by catching the exception using a catch block, but in the body of the catch block, throwing the exception using the throw command. The method must declare that it can throw the exception using the throws keyword in the signature of the method. For example:

 public void myOtherMethod() throws MyException1 {     try {        myMethod();     }     catch( MyException1 e ) {        e.printStackTrace();        throw e;     }  } 

In this example, the call to myMethod() can throw MyException1. If it does, myOtherMethod() catches it, prints its stack trace, and then rethrows it.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

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