Handling Exceptions
Methods called by a tag handler may throw exceptions. Exception
handling in a simple tag handler is very easy compared to how
it’s done for a classic tag handler, since all
processing takes place in the single doTag(
)
method.
You must catch exceptions and either deal with them, or rethrow them
wrapped in a JspException or the
JspTagException subclass. Both exception classes
have the same types of constructors:
public JspTagException( ); public JspTagException(String msg); public JspTagException(String msg, Throwable rootCause); public JspTagException(Throwable rootCause);
When you rethrow an exception that you have caught, use one of the two latter constructors and pass on the exception, since the root cause is often needed to figure out what the problem is. Most containers unwrap the exception chain and write stack traces for all of the exceptions in the chain to the application log file. When throwing exceptions, you should avoid the no-argument constructor, since it doesn’t let you say what’s wrong, and use the second one for exceptions generated internally in the tag handler, for instance to report an invalid input type or insufficient privileges.
An exception thrown by a custom action can be caught and handled in
the JSP page with the JSTL <c:catch> action,
as I described in Chapter 9. If
it’s not caught, it’s handled by
the JSP container by forwarding to a custom error page, if specified,
or to a container default page.
Calling the invoke( ) method of a fragment ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access