Programming Tutorials

The TryCatchFinally Interface in JSP

By: Jagan in JSP Tutorials on 2007-10-06  

The TryCatchFinally interface was introduced in the JSP 1.2 specification. This interface provides a way to gracefully handle exceptions that may occur during the processing of classic tags, regardless of whether the tag implements the Tag, IterationTag, or BodyTag interface.

TryCatchFinally.java

import javax.servlet.jsp.*;
public interface TryCatchFinally {
void doCatch(Throwable t) throws Throwable;
void doFinally();
}

The TryCatchFinally interface has two methods, doCatch() and doFinally(), in which you can place functionality that might typically be written into catch and finally blocks. For example, in the doCatch() method, you might choose to roll back a transaction, and in the doFinally() method, you might choose to close a file or a connection to a remote resource. In essence, tags should implement this interface if you want them to have more control over exception handling. Figure below shows a UML diagram of the tag life cycle for a tag that implements the TryCatchFinally interface. Next, we'll cover each of the methods in turn.

The doCatch() Method

The JSP specification guarantees that the doCatch() method will be called if an exception is thrown in the doStartTag() method, the tag's body content, or the doEndTag() method. Additionally, if the tag handler implements the IterationTag or BodyTag interface, the doCatch() method will be executed if an exception is thrown within the doAfterBody() and doInitBody() methods, respectively.

Something to notice is that the doCatch() method won't be called if an exception is thrown before the execution of the doStartTag() method-perhaps when the context or attributes are being set. Therefore, it's best not to put any logic into attribute setter methods that may cause an exception to be thrown.

If the exception should be propagated further up the calling stack, perhaps by a JSP error page, the doCatch() method can handle the exception as required and then subsequently rethrow the same or a new exception. This is useful because there's no way to tell the tag handler class to catch only specific subclasses of Exception in the same way you would when writing try-catch blocks in your code. Instead, the doCatch() method handles all exceptions, and it's up to us as tag developers to decide which to handle and which to rethrow.

During the tag life cycle, there are several opportunities where doCatch() or doFinally() might be called to handle an exception.

The doFinally() Method

When you write try-catch-finally blocks in regular Java code, the finally block always gets called, regardless of whether an exception was thrown. Similarly, the doFinally() method on the tag handler will always get called.

Although tag handlers are generally small components, there is still much that can go wrong, especially when you're dealing with databases and remote objects such as Enterprise JavaBeans. Implementing the TryCatchFinally interface is a way to build tags that are better equipped to deal with such problems; it will make your tag libraries more robust and resilient to failure. With this in mind, let's now take a look at how you can deploy these resilient tags and make them available for use in the easiest possible way.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in JSP )

Latest Articles (in JSP)