11.5. Handling Exceptions with the TryCatchFinally Interface

In JSP 1.1, you can trap exceptions that occur in each of your tag handling methods (doStartTag, doEndTag, etc.). But, what happens if an exception occurs during the processing of the body of the tag? What if you want to respond to exceptions in doStartTag, doEndTag, and doAfterBody the same way and don’t want to repeat your code?

JSP 1.2 answers these questions by providing a new interface called TryCatchFinally with two methods:

  • doCatch(Throwable t)

  • doFinally()

If your tag implements this interface and an exception occurs during any of the tag life-cycle methods or during the processing of the tag body, the system calls the doCatch method. Regardless of whether an exception occurs, the system calls the doFinally method when done executing the tag. Note, however, that this exception-handling behavior applies only to the tag life-cycle methods and the processing of the tag body. It does not apply to the setXxx attribute-setting methods.

Core Warning

The TryCatchFinally interface does not apply to the methods that set the tag attributes.


To illustrate this new exception-handling behavior, Listing 11.25 shows a tag that implements TryCatchFinally. The tag doesn’t actually output anything: it just prints a message to standard output when doStartTag, doEndTag, doCatch, and doFinally are called. Listing 11.26 shows the TLD file that declares the tag; no new syntax or entries are needed.

Listing 11.27 shows a JSP page that uses the new tag wrapped around a body that sometimes throws an exception (see Listing 11.28). Figure 11-9 shows the result when none of the tags throws an exception. Listing 11.29 shows the corresponding output— doStartTag, doEndTag, and doFinally are invoked but doCatch is not. Figure 11-10 shows the result when the second invocation of the tag throws an exception—the tag body generates no output but the page processing continues normally. Listing 11.30 shows the corresponding output— doStartTag, doEndTag, and doFinally are invoked each time and doCatch is invoked only in the case when the tag body threw the exception.

Figure 11-9. One possible result of catchTest.jsp.


Figure 11-10. Another possible result of catchTest.jsp.


Listing 11.25. CatchTag.java
package moreservlets.tags; 

import javax.servlet.*; 
import javax.servlet.jsp.*; 
import javax.servlet.jsp.tagext.*; 
import java.io.*; 

/** Tag that traces the life cycle of tags that 
 *  implement the TryCatchFinally interface. 
 */ 

public class CatchTag extends TagSupport 
                      implements TryCatchFinally {
  public int doStartTag() {
    System.out.println("CatchTag: start"); 
    return(EVAL_BODY_INCLUDE); 
  } 

  public int doEndTag() {
    System.out.println("CatchTag: end"); 
    return(EVAL_PAGE); 
  } 

  public void doCatch(Throwable throwable) {
    System.out.println("CatchTag: doCatch: " + 
                       throwable.getMessage()); 
  } 

  public void doFinally() {
    System.out.print("CatchTag: doFinally
"); 
  } 
} 

Listing 11.26. catch-tag-taglib.tld
<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE taglib 
 PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" 
 "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> 

<taglib> 
  <tlib-version>1.0</tlib-version> 
  <jsp-version>1.2</jsp-version> 
  <short-name>catch-tags</short-name> 
  <description> 
    A tag library that uses a simple tag to illustrate 
    the behavior of the TryCatchFinally interface. 

    From More Servlets and JavaServer Pages, 
    http://www.moreservlets.com/. 
  </description> 

  <!-- Define the catchTag tag. --> 
  <tag>
						<name>catchTag</name>
						<tag-class>
						moreservlets.tags.CatchTag
						</tag-class>
						<body-content>JSP</body-content>
						<description>
						Implements the TryCatchFinally interface and
						prints simple tag life-cycle information to the
						standard output.
						</description>
						</tag> 
</taglib> 

Listing 11.27. catchTest.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
<HTML> 
<HEAD> 
<TITLE>Some Random Numbers</TITLE> 
<LINK REL=STYLESHEET 
      HREF="styles.css" 
      TYPE="text/css"> 
</HEAD> 
<BODY> 
<TABLE BORDER=5 ALIGN="CENTER"> 
  <TR><TH CLASS="TITLE"> 
      Some Random Numbers 
</TABLE> 

<%@ taglib uri="/WEB-INF/catch-tag-taglib.tld" prefix="msajsp" %> 
<UL> 
  <LI><msajsp:catchTag>
						<%= moreservlets.Utils.dangerousMethod() %>
						</msajsp:catchTag> 
  <LI><msajsp:catchTag>
						<%= moreservlets.Utils.dangerousMethod() %>
						</msajsp:catchTag> 
  <LI><msajsp:catchTag>
						<%= moreservlets.Utils.dangerousMethod() %>
						</msajsp:catchTag> 
  <LI><msajsp:catchTag>
						<%= moreservlets.Utils.dangerousMethod() %>
						</msajsp:catchTag> 
  <LI><msajsp:catchTag>
						<%= moreservlets.Utils.dangerousMethod() %>
						</msajsp:catchTag> 
</UL> 
</BODY> 
</HTML> 

Listing 11.28. Utils.java
package moreservlets; 

public class Utils {
  public static String dangerousMethod() throws Exception {
    double num = Math.random(); 
    if (num < 0.8) {
      return("Random num: " + num); 
    } else {
      throw(new Exception("No intelligent life here.")); 
    } 
  } 
} 

Listing 11.29. Output of catchTest.jsp corresponding to Figure 11-9
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 

Listing 11.30. Listing 11.30 Output of catchTest.jsp corresponding to Figure 11-10
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: doCatch: No intelligent life here. 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 
CatchTag: start 
CatchTag: end 
CatchTag: doFinally 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.100