Manual Exception Handling, manual handling
When running the program, you may encounter some errors. At this time, you need to handle the errors so that they can be displayed to the user in a more friendly manner. So here we will introduce the use of manual error handling:1. Create a class for the error handler and inherit fromRuntimeException, as shown below
public class ApplicationException extends RuntimeException { public ApplicationException() { } public ApplicationException(String message) { super(message); } public ApplicationException(Throwable cause) { super(cause); } public ApplicationException(String message, Throwable cause) { super(message, cause); } public ApplicationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { super(message, cause, enableSuppression, writableStackTrace); }}
2. Error Handling Configuration
<! -- Configure the error jump page --> <error-page> <exception-type> com. bjpowernode. drp. util. applicationException </exception-type> <location>/error. jsp </location> </error-page>
3. error. jsp (simple interception)
<Div align = "center"> <p align = "left"> <span class = "STYLE1"> error message: </div> <% = exception. getMessage () %> </div>
4. In addition, some error codes are handled as follows:1) configuration of error codes in the configuration file
<! -- Configure the error jump page --> <error-page> <error-code> 404 </error-code> <location>/http_error.jsp </location> </error-page> <error-page> <error-code> 500 </error-code> <location>/http_error.jsp </location> </error-page>
2) handle the error handling page http_error.jsp
<Body> <% Integer errorCode = (Integer) request. getAttribute ("javax. servlet. error. status_code "); // when the error codes are 404 and 500 respectively, go to the corresponding error handling page to process if (errorCode = 404) {response. sendRedirect (request. getContextPath () + "/404.jsp");} else if (errorCode = 500) {response. sendRedirect (request. getContextPath () + "/500.jsp") ;}%> </body>
3) handle the error handling page. If it is 404: <body> the requested page is not found </body> if it is 500: <body> the system has an error, contact the system administrator! </Body>