<%@ page isErrorPage='true' %>
Using Exception Object:
The exception object is an instance of a subclass of Throwable (e.g., java.lang. NullPointerException) and is only available in error pages. Following is the list of important methods available in the Throwable class.
public String getMessage():
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
public Throwable getCause():
Returns the cause of the exception as represented by a Throwable object.
public void printStackTrace():
Prints the result of toString() along with the stack trace to System.err, the error output stream.
public StackTraceElement [] getStackTrace():
Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.
public Throwable fillInStackTrace():
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.
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
index.jsp
<%@page errorPage=”ErrorPage.jsp” %>
<%
String name = null;
int i = 9;
int j = 0;
out.print(i/j);
out.print(name.trim());
%>
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.
Example of exception handling in jsp by specifying the error-page element in web.xml file
This approach is better because you don’t need to specify the errorPage attribute in each jsp page. Specifying the single entry in the web.xml file will handle the exception. In this case, either specify exception-type or error-code with the location element. If you want to handle all the exception, you will have to specify the java.lang.Exception in the exception-type element. Let’s see the simple example:
- web.xml file for specifying the error-page element
- index.jsp for input values
- index.jsp for dividing the two numbers and displaying the result
- ErrorPage.jsp for displaying the exception
web.xml file if you want to handle any exception
<web-app> <error-page> <exception-type>java.lang.Exception</exception-type> <location>/ErrorPage.jsp</location> </error-page> </web-app>
This approach is better if you want to handle any exception. If you know any specific error code and you want to handle that exception, specify the error-code element instead of exception-type as given below:
web.xml file if you want to handle the exception for a specific error code
<web-app> <error-page> <exception-type>500</exception-type> <location>/ErrorPage.jsp</location> </error-page> </web-app>