Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

RE: How is catch{} code handled

Craig Berry

2004-01-02


Merrill Cornish [mailto:merrill.cornish@(protected)...

> Why did main servlet processing appear to continue (allowing the
> second sendRedirect() to cause a problem) after the exception was
> triggered?

Processing of a catch block does not terminate processing of the surrounding method. In other words, in this code fragment

try {
  foo.callMethodThrowingFooException();
}
catch (FooException fe) {
  System.out.println("Caught a FooException");
}
System.out.println("After try/catch block");

if a FooException if thrown, control flows to the catch block, but then drops out to the following code normally. This is a useful Java language feature for dealing with locally-recoverable errors.

If you want method processing to stop, you have to throw a new exception, return from inside the catch block, add logic suppressing further processing triggered by a flag set in the catch block, or otherwise explicitly avoid executing the remainder of the method.

©2008 junlu.com - Jax Systems, LLC, U.S.A.