ASP. NET Exception Handling

Source: Internet
Author: User

In Asp.net During the development process, we will encounter many Exception , Do not process these Exception The page appears.
There are some unexpected Exception When Exception We must also record the specific location so that we can correct the error.
Asp.net The Exception Handling Location is roughly as follows: 3 Location
1. ProgramOfCodeSegment, which is the most direct way to handle exceptions. As follows:
Try
{
N = convert. toint32 (Info );
}
Catch (exception)
{
}
Only the most basic exception handling.

2. ASP. NET In Application_error Medium . Application_error Event. This event is triggered for any unprocessed exceptions thrown in the application. Generally, we handle the following:
Protected void application_error (Object sender, eventargs E)
{
Exception exp = server. getlasterror ();
String stre =" Internal error : "+ Exp. innerexception. tostring () +" \ r \ n STACK: "+ Exp. stacktrace +" \ r "+" message: "+ exp. Message +" \ r Source : "+ Exp. source;

// Record the exception information in the event log
Log (stre );
Server. clearerror ();
Server. Transfer ("error. aspx", false );
}
In this way, we can handle Server Errors. We record the source of the error.

3. You can also handle code errors at the page or application level. Page The base class exposes Page_error Method, which can be overwritten on the page. This method is called whenever an uncaptured exception is thrown during running.
Void page_error (Object source, eventargs e ){
String message = "<font face = verdana color = Red>"
+ "<H4>" + request. url. tostring () + "</H4>"
+ "<PRE> <font color = 'red'>"
+ Server. getlasterror (). tostring () + "</PRE>"
+ "</Font> ";

Response. Write (Message );
}

 

Next I will explain howASP. NETWe use the most commonSessionExpiration example

Let's writeSessionExpired exception
Public class ysessionexception: exception
{
}

Let's define another attribute.
Public int sessionvalue
{
Get {If (session ["sessionvalue"] = NULL)
{
Throw new ysessionexception ("");
}
}
}

Below we Page_error Or Application_error Processing this exception
{
Exception exp = server. getlasterror ();
If (exp is ysessionexception)
{
..................
}
Server. clearerror ();
Server. Transfer ("error. aspx", false );
}

In this way, we can provide a good programExceptionProcessing interface.

 

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.