Passing an Exception Through Your Method


Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 9.  Exception Handling

Not only can you catch and rethrow an exception, you can ignore it altogether by adding the exception class to your method's exception list. This way if an exception of a certain type occurs, it can be passed directly to the method that called you. Eventually someone will have to catch the exception and deal with it, but you can defer that to the calling method. If we were to skip printing the stack trace for the exception in the previous example and let the exception pass through to our caller, we can rewrite myOtherMethod() as follows:

 public void myOtherMethod() throws MyException1 {        myMethod();  } 

Thus, if myMethod() throws a MyException1 exception, it is passed to the caller of myOtherMethod().


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

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: