JSP exception handling

When writing JSP programs, programmers may miss some bugs, and these bugs may appear anywhere in the program. JSP code usually has the following exceptions:

  • Checked exception: A checked exception is a typical user error or an error that a programmer cannot foresee. For example, if a file is about to be opened but the file cannot be found, an exception is thrown. These exceptions cannot simply be ignored at compile time.
  • Runtime exceptions: A runtime exception may have been avoided by the programmer, and such exceptions will be ignored at compile time.
  • Error: An error is not an exception, but the problem is that it is beyond the control of the user or programmer. Errors are usually ignored in your code, and you can hardly get at it. For example, a stack overflow error. These errors are ignored at compile time.
  • This section will give a few simple and elegant ways to deal with runtime exceptions and errors.

    Using Exception Objects

    The exception object is an instance of the Throwable subclass and is only available on error pages. The following table lists some important methods in the Throwable class:

    Serial number method&Description
    1 public String getMessage ()

    Returns exception information. This information is initialized in the Throwable constructor
    2 public ThrowablegetCause ()

    Returns the cause of the exception, type Throwable object
    3 public String toString ()

    Return class name
    4 public void printStackTrace ()

    Output the exception stack trace to System.err
    5 public StackTraceElement [] getStackTrace ()

    Returns the exception stack trace as an array of stack trace elements
    6 public ThrowablefillInStackTrace ()

    Fill the Throwable object with the current stack trace

    JSP provides the option to specify an error page for each JSP page. Whenever an exception is thrown by the page, the JSP container will automatically call the error page.

    The following example specifies an error page for main.jsp. use <%@page errorPage="XXXXX"%>The directive specifies an error page.

    <%@ page errorPage="ShowError.jsp" %>
       <title>Error Handling Example</title>
       // Throw an exception to invoke the error page
       int x = 1;
       if (x == 1)
          throw new RuntimeException("Error condition!!!");

    Now, write the ShowError.jsp file as follows:

    <%@ page isErrorPage="true" %>
    <title>Show Error Page</title>
    <p>Sorry, an error occurred.</p>
    <p>Here is the exception stack trace: </p>
    <% exception.printStackTrace(response.getWriter()); %>

    Noticed´╝îShowError.jsp file used<%@page isErrorPage="true"%>Directive, this directive tells the JSP compiler to generate an exception instance variable.

    Now try to visit the main.jsp page, it will produce the following result:

    java.lang.RuntimeException: Error condition!!!
    Sorry, an error occurred.
    Here is the exception stack trace:

    Using JSTL tags in error pages

    You can use JSTL tags to write the error page ShowError.jsp. The code in this example is almost the same as the code in the previous example, but the code in this example has a better structure and can provide more information:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%@page isErrorPage="true" %>
    <title>Show Error Page</title>
    <table width="100%" border="1">
    <tr valign="top">
    <td width="40%"><b>Error:</b></td>
    <tr valign="top">
    <tr valign="top">
    <td><b>Status code:</b></td>
    <tr valign="top">
    <td><b>Stack trace:</b></td>
    <c:forEach var="trace" 

    The results are as follows: