The following is referenced from http://wiki.jikexueyuan.com/project/jsp/exception-handling.html:
When writing JSP code, it is possible to leave a coding error, and it will appear in any part of the code. There are the following types of errors in the JSP code:
Detect exception (Checked exceptions): A detection exception is usually a user error or an exception caused by an error that the programmer could not foresee. For example, if you open a file but cannot find the file, an exception occurs. These exceptions cannot be simply ignored at compile time.
Run exception (runtime Exceptions): A running exception could be an exception that the programmer could have avoided. As opposed to detecting exceptions, run exceptions can be ignored at compile time.
- Error (Errors): This is not an exception, it is a problem that is beyond the control of the user or programmer. Errors are usually ignored in the code because there is little that can be done about a bug. For example, if an overflow occurs on a stack, an error occurs. They will also be ignored at compile time.
First, using the exception object
An exception object is an instance of the Throwable subclass (for example, Java.lang.NullPointerException), which can only be available on the error page. The following is a list of the important methods available in the Throwable class.
| Method |
Description |
| Public String GetMessage () |
Returns the details of the exception that occurred. The message is initialized in the Throwable constructor. |
| Public Throwable Getcause () |
Returns the reason for the occurrence of an exception, expressed as a Throwable object. |
| Public String toString () |
Returns the class name that is connected to the GetMessage (). |
| public void Printstacktrace () |
Output tostring () and stack trace of system.err result, error output stream. |
| Public Stacktraceelement [] Getstacktrace () |
Returns an array that contains each element of a stack trace. An element with an index value of 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 the Throwable object with the current stack trace, adding any previous stack trace information. |
The JSP gives an option to specify the error page for each JSP. Whenever a page throws an exception, the JSP container automatically invokes the error page.
The following is an example of a specific error page in main.jsp. In order to create an error page, use the <%@ page errorpage= "xxx"%> directive.
<%@ Page ErrorPage="showerror.jsp" %><HTML><Head> <title>Error Handling Example</title></Head><Body><% //Throw an exception toInvoke theErrorpageintx= 1; if(x== 1) {ThrowNewRuntimeException ("Error Condition!!!"); }%></Body></HTML>
Now you need to write an error-handling JSP:ShowError.jsp, and here's the code. Note that the error handling page includes the <%@ page iserrorpage= "true"%> directive. This directive causes the JSP compiler to generate exception instance variables.
<%@ Page Iserrorpage="true" %><HTML><Head><title>Show Error Page</title></Head><Body><H1>Opps ...</H1><P>Sorry, an error occurred.</P><P>Here is the exception stack trace:</P><Pre><%Exception.printstacktrace (Response.getwriter ());%></Pre></Body></HTML>
Now trying to access main.jsp, it will produce the following results:
Second, use the JSTL label on the error page
You can use the JSTL tag to write an error page showerror.jsp. This page and the example above use almost the same logic, but it has a better structure, and he provides more information:
<%@ taglib Prefix="C"URI="Http://java.sun.com/jsp/jstl/core" %><%@page iserrorpage="true" %><HTML><Head><title>Show Error Page</title></Head><Body><H1>Opps ...</H1><Tablewidth= "100%"Border= "1"><TRvalign= "Top"><TDwidth= "40%"><b>Error:</b></TD><TD>${pagecontext.exception}</TD></TR><TRvalign= "Top"><TD><b>Uri:</b></TD><TD>${pagecontext.errordata.requesturi}</TD></TR><TRvalign= "Top"><TD><b>Status Code:</b></TD><TD>${pagecontext.errordata.statuscode}</TD></TR><TRvalign= "Top"><TD><b>Stack Trace:</b></TD><TD><C:foreachvar= "Trace"Items= "${pagecontext.exception.stacktrace}"> <P>${trace}</P></C:foreach></TD></TR></Table></Body></HTML>
Now trying to access main.jsp, it will produce the following results:
Third, use Try ... Catch block
If you want to handle errors in the same page, using some actions instead of releasing an error page, you can take advantage of the Try...catch block.
A simple example of how to use the Try...catch block is shown below. Put the following code in the main.jsp:
<HTML><Head> <title>Try ... Catch Example</title></Head><Body><%try{intI= 1; I=I/ 0; Out.println ("The answer is" +i); } catch (Exception e) {out.println ("An exception occurred:" +e.getmessage ()); }%></Body></HTML>
Now trying to access main.jsp, it will produce the following results:
Test Project: https://github.com/easonjim/5_java_example/tree/master/jspbasics/test22
Exception handling for JSP