How to handle exceptions in ASP. NET

Source: Internet
Author: User
Tags sql error
Exception generation

In. NET application development,ProgramDuring running, an exception is thrown due to unexpected and invalid objective conditions. For example, during type conversion, a non-numeric character is converted to an integer; when a reference type is not initialized, we call its method or attribute; when I/O operations are performed, resources are not ready; when database operations are performed, the DB server cannot be accessed, or when an SQL error occurs. 

How does ASP. NET handle exceptions by default?

When an exception occursCodeWhen an exception is caught, the application will send the exception information to Asp.net runtime and throwHttpunhandledexception is abnormal. This exception is reported to the yellow pages that we often see. Of course, it does not only report yellow pages, but can also customize abnormal pages based on the information in the configuration.

In the webconfig configuration file, <customerrors> this node defines the processing behavior of the httpunhandledexception class. Different behaviors depend on the mode attribute of the customerrors node. There are three attributes:

    1. On displays custom error pages or yellow pages of a runtime error for all users, excluding error details.
    2. Off exposes detailed error information to all Access Users
    3. Remoteonly exposes friendly error messages to non-local users.

When customizing the error page, you can <customerrors mode = "on" defaultredirect = "~ /Error. aspx "/> defines an error page. You can also specify different error pages for different exception types, such: 

         <  Customerrors  Mode  = "On"  Defaultredirect  = "Error. aspx"  >              <  Error Statuscode  = "404"  Redirect  = & Quot; 404. aspx & quot"  />              <  Error  Statuscode  = "500"  Redirect  = & Quot; 500. aspx & quot"  />          </  Customerrors  > 

In this case, different pages are redirected based on different HTTP codes.

Add another sentence:

    1. Httpunhandledexception usesResponse. Redirect () method 302 jump.
    2. It is true that some static resource files are not processed in 404 cases. These files are directly processed by IIS.
    3.  
404. What should I do?

In the case of 404, sometimes the website link is incorrectly written, or the external link is incorrect from some other sites. At this time, we know where the link is important, so we 'd better tell ourselves what happened and where it happened on the page jump after 404 and handle it in time. When I started thinking about this problem, I still felt that the source of the error page could not be obtained using urlref. And then sentWhen the response. Redirect () method is redirected to 302, some information about the original request is also included. So I dispelled this doubt. In this way, we can record the exception source on the 404 exception handling page to notify ourselves. In addition, we cannot wait for them to modify the error links from some external sites. We can map the error links and perform matching on the 404 processing page.

404 we have obtained the source of the dead link. How can we obtain detailed error information?

One problem is that when we define a friendly error page, we cannot get detailed error information. For example. if an exception occurs on the ASPX page, the page will jump back to 500. the ASPX page is equivalent to two requests. In the new request (302 jump), we cannot obtain the information of the previous page. Let's take a look at how we can handle this situation.

When an unhandled exception is thrown to Asp.net runtime, an application-level exception event is triggered. by obtaining the exception in this event, we can record the log, send an email to notify the author, and useServer. Transfer () redirects to the custom error page. Transfer is different from the Redirect method. It redirects to the server and carries context information, allowing the redirected page to access this information .;)

How to handle exceptions in application-level exception events

The httpapplication class contains a series of Asp.net web application events. When an unhandled exception occurs, an error event of this class is triggered. In this event, we can useServer. getlasterror () method to obtain the exception object. Then you can record and notify the exception information. The exception object we get here is an instance of httpunhandledexception. The exception occurred on the actual page is encapsulated in this object. You can useThe innerexception attribute is obtained.

In this way, we can capture the exception information of the application and record and send emails, and then jump to the custom error page.

         //  Get the exception object on the page          VaR Error = Server. getlasterror ();  If (Error Is Httpunhandledexception & error. innerexception! = Null  ) {Error = Error. innerexception ;}  Try  {  //  Do something with error Infomation  }  Catch {} Server. Transfer (  "  Yourcustompage. aspx  " );
Handling exception events using httpmodule

Why use httpmodule?

This method provides a more flexible processing method. We can simply put the DLL in the bin directory and modify a few configuration files to add our functions, which is very flexible and free.

Open-source logstore: elmah

 

 

 

Reference Abstract:

Http://www.4guysfromrolla.com/articles/090606-1.aspx

Http://www.4guysfromrolla.com/articles/091306-1.aspx

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.