O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

You can make the exception an attribute

image with no caption

In a real Java try/catch, the catch argument is the exception object. But with web app error handling, remember, only officially-designated error pages get the exception object. To any other page, the exception just isn’t there. So this does not work:

image with no caption

Using the “var” attribute in <c:catch>

Use the optional var attribute if you want to access the exception after the end of the <c:catch> tag. It puts the exception object into the page scope, under the name you declare as the value of var.

image with no caption

Note

Flow control works in a <c:catch> the way it does in a try block—NOTHING runs inside the <c:catch> body after the exception.

In a regular Java try/catch, once the exception occurs, the code BELOW that point in the try block never executes—control jumps directly to the catch block. With the <c:catch> tag, once the exception occurs, two things happen:

  1. If you used the optional “var” attribute, the exception object is assigned to it.

  2. Flow jumps to below the body of the <c:catch> tag.

image with no caption

Be careful about this. If you want to use the “var” exception object, you must wait until ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required