JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»
6.9.2    Exceptions and cleanup

What happens if an exception is thrown somewhere within this life cycle? Most of the tag’s methods can throw a JspException, but the method may (of course) throw a runtime exception such as java.lang.NullPointerException. What then? The answer is rather simple. If doStartTag (), doEndTag(), or some other tag callback method was called and threw an exception, in JSP1.1 the JSP runtime would immediately call release(), not doEndTag() . This is not per the specification, but is the common practice and a reasonable solution since the tag should not

gather state until the call to dostartTag(). For example, look at the following pseudocode fragment that is similar to that generated by Tomcat’s JSP1.1 translator.

Sometag _t = get Sometag (); t.setPageContext(pageContext); t.setParent(null); t.setSomeProperty(…);

try {


// some code was omitted … t.doEndTag();

} finally {



As you can see, release() is executing within a finally block, assuring us that it will be called even in the face of exceptions.

JSP1.2 offers an improved and regulated exception handling capability by providing the TryCatchFinally interface. Tags that implement TryCatchFinally inform the JSP runtime that they want to be notified when exceptions occur during their run. The JSP runtime will assure that the TryCatchFinally methods in the tags will be called in the appropriate time.

TryCatchFinally and the JSP runtime

As stated in chapter 4, the TryCatchFinally interface exports the following methods:

■    doCatch() allows the JSP runtime to inform the tag that an exception was thrown while executing it.

Скачать в pdf «JSP Tag Libraries»