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 ...

Get Head First Servlets and JSP, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.