The TryCatchFinally Interface in JSP

By: Jagan Emailed: 1788 times Printed: 2615 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

package javax.servlet.jsp.tagext;
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.


JSP Home | All JSP Tutorials | Latest JSP Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jagan
Importing a Certificate for the CA example using keytool in Java
The if-then-else Statement in Java
WHERE Clauses in SQL
The TryCatchFinally Interface in JSP
assert() Function Example program in C
Guidelines for Struts Application Development
Using printf function in C
File Inclusion in C
Using Multiple Message Resource Bundles in Struts
While and For Loops in C
Arrays sample program in C
Controlling Page Navigation in JSF - Static and Dynamic Navigation
Using cout.fill() in C++
Types of Profiles in J2ME
DateField sample program in J2ME

More Tutorials in JSP
LifecycleException: service.getName(): "Catalina"; Protocol handler start failed: `java.net.BindException: Permission denied <null>:80
JSP Alert Example
JSP CheckBox Example
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
A JSP page that gets properties from a bean
The page Directive in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Declarations in JSP
Scriptlets and Expressions in JSP
Tag Libraries in JSP
The Request Object in JSP
The Response Object in JSP
The Out Object in JSP
The Session Object in JSP

More Latest News
Most Viewed Articles (in JSP )
JSP Example to connect to MS SQL database and retrieve records
What are the different scopes in JSP?
JSP CheckBox Example
JSP Alert Example
Comparison operators in JSP
Sending Email using JSP
Uploading an Image to a Database using JSP
Arithmetic Evaluation Using the Expression Language in JSP
The Advantages of JSP
Disabling Scriptlets in JSP using web.xml
The Request Object in JSP
Writing your first JSP page
Uploading a file to a server using JSP
Deploying an Individual JSP on Tomcat
Automatically Refreshing a JSP
Most Emailed Articles (in JSP)
Calling JSP from a batch file
Simple HTML-Building Utilities
Handling Events
The Tag Life Cycle with Attributes in JSP
JSP Program for display Date
Sessions in JSP
NetworkClient
The Advantages of JSP
Using a DataSource from WebLogic in a JSP
The JSP Program running first Time.
JSP pages in servlet
Combining Scriptlets with HTML
Declaring variable in JSP
An Example Using Servlet Initialization and Page Modification Dates
Password Object