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.