ASP. NET error handling mechanism-convert

Source: Internet
Author: User
Pair Web Application Program Unpredictable errors and exceptions are inevitable. We must provide error handling mechanisms for web programs. When an error occurs, we must do two things: First, record the error information and send an email to the website maintenance personnel to facilitate the technical personnel to track and handle the error. Second, it prompts the end user that an error has occurred on the page in a friendly way, you cannot display unprocessed error messages to users.

Let's think about how many error handling mechanisms ASP. NET provides for us? If they are used at the same time, is there a certain priority ?. NET provides four error handling mechanisms, which have a certain priority order: Page_error event>Errorpage attributes>Application_error event><Customerrors> configuration item . The following describes the usage of these four error handling mechanisms.

1. page_error event
The page_error event provides a method to capture page-level errors. You can only display the error message (as shown in the following example)Code You can also record events or perform some other operations. Private   Void Page_load ( Object Sender, system. eventargs E)
{
//Put user code to initialize the page here
Throw NewException ("Page error!");
}

Protected   Void Page_error ( Object Sender, eventargs E)
{< br> exception objerr = server. getlasterror (). getbaseexception ();
response. write ( " error: " + objerr. message);
server. clearerror (); / pay attention to the use of this Code
}

Remarks: This example displays detailed error information in the browser. This example is only for instructions. Be careful when displaying detailed information to end users of the application. A more appropriate way is to display a message to the user, tell the user that an error has occurred, and then record the detailed error information in the log.

2. errorpage attributes
You can set the errorpage attribute almost anytime on the page to determine which page will be redirected when an error occurs.To enable the errorpage attribute to play a role, the mode attribute in the <customerrors> configuration item must be set to "on ".

This . Errorpage =   " ~ /Errorhandling/pageerror.html " ;

If both page_error and errorpage exist, what is the page execution sequence when an exception is thrown? The page will first execute the page_error event processing function, if the page_error () event calls the Function Server. Clearerror () If the error message is cleared, the specified page of The errorpage attribute is not displayed. If server. clearerror () is not called, the error message is displayed, and the specified page of The errorpage is displayed. This proves the priority order: Page_error event>Errorpage attributes .

3. application_error event
Similar to the page_error event, you can use the application_error event to capture errors that occur in the application. As events occur throughout the application, you can record application error messages or handle other possible application-level errors. Add the following code to the Global. asax file. Protected   Void Application_error ( Object Sender, eventargs E)
{
Exception ex = Server. getlasterror (). getbaseexception ();
// In actual application, exception information can be recorded or savedDatabaseMedium
// You can also send an error email to the website maintenance personnel.
Response. Write ( " Error: "   + Ex. Message );
// Clear the exception and avoid passing it to the upper-level for processing.
// The upper-level section is the <mermererrors> Configuration section.
Server. clearerror ();
}

4. <customerrors> configuration item
In the <customerrors> Configuration section of the configuration file web. config, you can specify the redirection page as the default error page defaultredirect or specify a specific page based on the HTTP Error code. If an error is not captured at any level before the application, this custom page is displayed. < Customerrors Mode = " On " Defaultredirect = " ~ /Errorhandling/applicationerror.html " >
< Error statuscode = " 404 " Redirect = " ~ /Errorhandling/404.html "   />
  </ Customerrors >


Similarly, if application_error and <customererrors> both exist, there are also execution sequence problems. Because the priorityApplication_error event> <Customerrors> configuration itemTherefore, when an application-level error occurs, the code in the application_error event is preferentially executed.Server. clearerror ()Function. defaultredirect in the <customererrors> Configuration section does not work because the exception has been cleared. If the application_error event is not calledServer. clearerror ()Function, the error page will go to the URL page specified by defaultredict to display user-friendly error information.

By analyzing the above four error handling mechanisms provided by. net, we can classify them from different perspectives for our understanding and use.
1. Functional classification:Handling exceptions)It is a page_error event and application_error event;User error page redirection (redirecting the user to an error page)Is the errorpage attribute and <customerrors> configuration item.
2. classification from error handling scope:Used for pagesLevel (page level)The error is handled by the page_error event and errorpage attributes;ApplicableApplication Level)Errors are handled by application_error events and <customerrors> configuration items.

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.