Rethrowing Exceptions in Java Programming

Rethrowing Exceptions in Java Programming

Previous Home Next

 

Sometimes you’ll want to Rethrow the exception that you just caught, particularly when you use Exception to catch any exception. Since you already have the handle to the current exception, you can simply re-throw that handle .

 If you simply re-throw the current exception, the information that you print about that exception in 

printStackTrace( )
will pertain to the exception’s origin, not the place where you re-throw it. If you want to

install new stack trace information, you can do so by calling fillInStackTrace( ), which returns an exception

object that it creates by stuffing the current stack information into the old exception object.

 Syntax of Rethrowing Exception
catch(Exception e)
 {

System.out.println("An exception was thrown");
throw e;
}



 

Previous Home Next