Rethrowing Exceptions

Contact Us or call 1-877-932-8228
Rethrowing Exceptions

Rethrowing Exceptions

An exception may be rethrown.

When we throw an exception, it does not necessarily have to be a new object. We can reuse an existing one.

This allows us to partially process the exception and then pass it up to the method that called this one to complete processing. This is often used in servlets and JSPs to handle part of the problem (possibly just log it), but then pass the problem up to the servlet or JSP container to abort and send an error page.

String s = "1234X";
try {
	Integer.parseInt(s);
} catch (NumberFormatException e) {
	System.out.println("Bad number, passing buck to JVM");
	throw e;
}

The stack trace will still have the original information. The fillInStackTrace method for the exception object will replace the original information with information detailing the line on which fillInStackTrace was called as the origin of the exception.

Next