ASP. NET Error Handling Mechanism

Source: Internet
Author: User

For a Web application, errors are inevitable. Therefore, we should plan ahead to provide appropriate solutions for possible errors. In fact, a good error handling mechanism is an important criterion for measuring the quality of Web applications. Think about it. If the user accidentally enters the wrong URL in the browser or when the user provides some information that leads to a program error, if we do not handle the problem, instead, the stack information of 404 or 500 error pages or even errors is displayed in front of the user, which will undoubtedly scare some users away. Therefore, when developing Web applications, we should have a full understanding of ASP. NET error handling mechanisms.

Let's go back to ASP. NET and raise two questions for everyone to think about: what are the ASP. NET error handling mechanisms? If several error handling mechanisms are adopted at the same time, is there a certain priority between them? With this problem, let's take a look at our most common Web. Config file:

 
 
  1. <?xml version="1.0"?>  
  2. <configuration>  
  3.  <system.web>  
  4.   <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">  
  5.     <error statusCode="403" redirect="Error403.htm" />  
  6.     <error statusCode="404" redirect="Error404.htm" />   
  7.   </customErrors>  
  8. </system.web>  
  9. </configuration> 

For the <customErrors> setting item, I don't need to talk about it any more. For details, refer to MSDN. The first error handling mechanism-using the <customErrors> configuration item of Web. Config should be the most commonly used.

Next, let's look at another commonly used file: Global. asax. What do you think of when talking about this file? Yes, it is related to the Application and Session of two Web Application objects. Among these events, there is an Error-related event in the Application scope, and the corresponding event processing method is Application_Error. As the name suggests, this event processing method will be called when an application-level error occurs. Therefore, you can add code to this method to handle the error, as shown below:

 
 
  1. protected void Application_Error(object sender, EventArgs e) {  
  2.  Exception objErr = Server.GetLastError().GetBaseException();  
  3.  Response.Write("Error:" + objErr.Message);  
  4.  Server.ClearError();  

Here, we should pay attention to the use of the last code Server. ClearError (). Why should we use this code? What if I don't need it? Here I want to sell another token. Well, the second type of error handling mechanism-the Application_Error event handling method in Global. asax is also on stage.

The preceding two error handling methods can be said to be Global. One is from the application configuration file, and the other is the event processing method that must be placed in the Global. asax file under the application root directory. Compared to the global one, it is local, so we naturally think: Is there any error handling mechanism applied to a local page? The answer is "yes", and there are two other methods: Use the ErrorPage attribute and use the Page_Error event processing method. For the first mechanism, you can set the ErrorPage attribute almost anytime to determine the page to which the page will be redirected when an error occurs. For the second mechanism, it is similar to the Application_Error event processing method, except that the trigger time is different. The following are two examples:

 
 
  1. <ScriptLanguage="C #" Runat="Server">
  2. Protected void Page_Load (object sender, EventArgs e ){
  3.  This. ErrorPage="ErrorPage.htm";
  4. }
  5. </Script>
  6.  
  7. Protected void Page_Error (object sender, EventArgs e ){
  8. ExceptionObjErr=Server. GetLastError (). GetBaseException ();
  9. Response. Write ("Error:" + objErr. Message );
  10. Server. ClearError (); // pay attention to the use of this Code.
  11. }

So far, all the four ASP. NET error handling mechanisms have been available, and it is time to rank them. From high priority to low priority: Page_Error event handling method> ErrorPage attribute> Application_Error event handling method> <customErrors> configuration item. Although the sorting is like this, there is a subtle relationship between the sorting. First, to enable the ErrorPage attribute to play a role, the mode attribute in the <customErrors> configuration item must be set to "On". Second, although the Page_Error event processing method is at the top, if the Server is missing. clearError () method still causes lower-priority error handling, which is also true for Application_Error events. The order is arranged, but the order is not the most important. It can be said that there is not much significance, because in many cases, you may not mix these four processing mechanisms. I think the most important question is how to use these error handling mechanisms. We hope that experienced users can talk about this issue.

Now, let's talk about the four ASP. NET error handling mechanisms. The designers of ASP. NET have made comprehensive considerations from the developer's perspective. Therefore, they provide up to four error handling mechanisms for us to choose from. This is commendable. However, when we use an advertisement word-many confusing words, we will also feel dizzy with so many error handling mechanisms. Comparing with the error handling in the J2EE field, we can find that it is relatively simple. First, it corresponds to the <customErrors> Settings. find similar configuration items in the xml file: <errorPage>. Second, in the J2EE field, Page is not an important entity and the event-driven model is not required, therefore, I still cannot find the processing mechanism corresponding to the Application_Error and Page_Error methods. Finally, in the J2EE field, more emphasis is placed on Request and Response, once an error occurs in logic processing, we can easily distribute the Request to the corresponding error processing module through RequestDispatcher. In fact, this is a very flexible way to handle it, if you are interested, please take a look.

  1. The first test of WCF, using JQuery to implement the loading Function
  2. JQuery calls the WCF Service to pass JSON objects
  3. Transmission Security Mechanism of WCF
  4. Detailed explanation of Change Handling in WCF: Unknown Best Practices
  5. Use ASP. net ajax to call the WCF Service item Template

Related Article

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.