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 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.
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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
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:
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.
Most Viewed Articles (in JSP )
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Steps to get a Free SSL certificate for your Tomcat
Uploading a file to a server using JSP
Server Side Programming using JSP
Latest Articles (in JSP)
Show a calendar for user input in JSP
Steps to get a Free SSL certificate for your Tomcat
Encrypting Passwords in Tomcat using Servlets
JSP Tags for SQL to connect to a database
Uploading a file to a server using JSP
Uploading an Image to a Database using JSP
A JSP page that gets properties from a bean
Scriptlets and Expressions in JSP
The taglib, tag, include, attribute and the variable Directive in JSP
Show a calendar for user input in JSP
Encrypting Passwords in Tomcat using Servlets
Steps to get a Free SSL certificate for your Tomcat
JSP Tags for SQL to connect to a database
Uploading an Image to a Database using JSP
Uploading a file to a server using JSP
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate