Home  |   STIU  |   WOW  |   SCJP  |   SCDJWS   |   JEE FAQ  |   About US  |  

FAQ
  Java FAQ
  JSP FAQ
  Servlet FAQ
 

Advertisement

Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Advertisement
XyzWs JSP FAQ: What is the exception implicit object?

What is the exception implicit object?


The exception object represents all errors and exceptions. The exception implicit object is of type java.langThrowable. You can access the exception object on a page that you declare to be an error page using the isErrorPage attribute of the page directive. Note that the exception object is created only if the JSP uses the page directive to set isErrorPage set to true. When a JSP generates an error and forwards that error to the error page, it sets the JSP exception object of the error page to the generated error

<%@ page isErrorPage='true' %>

The following table describes common methods of the exception object:

Method Description
getMessage Returns a string containing the error message.
printStackTrace Writes the exception object and its backtrace to standard error.
toString Returns a string describing the exception object.

The following code demonstrates the use of the exception implicit object. The first page uses the errorPage directive to set up the JSP page to use when an error occurs, and the second page called ErrorPage.jsp uses the isErrorPage directive to set itself up to catch the error.

First page

<%@page errorPage="ErrorPage.jsp" %>
<%
  int i=5;
  int j =0;
  out.print(i/j); 
%>

ErrorPage.jsp

 
<%@ page isErrorPage='true' %>
<%
out.print("Error Message : ");  
out.print(exception.getMessage());
%>

There are two kind of errors, compile-time errors and Runtime errors, can occur in the JSP lifecycle:

  • Compile-time errors : Compile-time errors occur when a client first invokes a JSP. JRun transforms the JSP into its XML view, and then compiles a servlet from the XML view. If an error occurs at any time during this process, a compile-time error is thrown. You must catch that error outside of the JSP. However, if you do not test or precompile the JSP, you might not catch the error at all.
  • Runtime errors : Runtime errors occur when a JSP successfully compiles but then tries to process code that contains an invalid statement; for example, when you try to include a nonexistent file. Runtime errors are easy to handle because you know that the JSP compiled successfully, you just need to catch the error and examine the output.

JSPs provide robust error-handling capabilities. You can use any Java code error handling techniques, such as try-catch blocks. You can also use the page directive to designate an error page, another JSP that handles the exception.


Previous Next vertical dots separating previous/next from contents/index/pdf Contents

Support  | Feedback  | Help