Manual Exception Handling, manual handling

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.