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
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
Comments