The <c:catch> tag. Like try/catch...sort of

image with no caption

If you have a page that invokes a risky tag, but you think you can recover, there’s a solution. You can do a kind of try/catch using the <c:catch> tag, to wrap the risky tag or expression. Because if you don’t, and an exception is thrown, your default error handling will kick in and the user will get the error page declared in the DD. The part that might feel a little strange is that the <c:catch> serves as both the try and the catch—there’s no separate try tag. You wrap the risky EL or tag calls or whatever in the body of a <c:catch>, and the exception is caught right there. But you can’t assume it’s exactly like a catch block, either, because once the exception occurs, control jumps to the end of the <c:catch> tag body (more on that in a minute).

image with no caption
image with no caption

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.