Exception Handling in ASP. NET:
When an exception is not processed in the call stack and is not processed by the Framework Code, we say that the exception is not handled and it will be processed by ASP. net capture, Asp.. net.
There are two events that will be called by ASP. NET when the exception is not handled:
Page_error event, which provides a method to capture errors at the page level.
Application_error event, which provides methods to capture errors anywhere in the code. The event scope is the entire application, which makes it an ideal place to add Log Code.
The execution sequence of these two events is -- execute page_error first, and then execute application_error. If you want to avoid handling errors in application_error, you can use server after handling errors in page_error. the clearerror method clears the last error to avoid calling application_error.
ASP. NET Ajax updatepanel exception handling:
You can use reflector to decompile the updatepanel control to view its implementation code and find that the exception on the updatepanel control has been handled in the onpageerror of the requestmanager class, therefore, you cannot capture the error in updatepanel in application_error for further processing. The detailed explanation is as follows:
Renderchildren (htmltextwriter): void method of the updatepanel Control
Protected override void renderchildren (htmltextwriter writer)
{
If (this. _ asyncpostbackmode)
{
If (this. _ rendered)
{
Return;
}
Htmltextwriter writer2 = new htmltextwriter (New stringwriter (cultureinfo. currentculture ));
Base. renderchildren (writer2 );
Pagerequestmanager. encodestring (writer, "updatepanel", this. clientid, writer2.innerwriter. tostring ());
}
Else
{
Writer. addattribute (htmltextwriterattribute. ID, this. clientid );
If (this. rendermode = updatepanelrendermode. Block)
{
Writer. renderbegintag (htmltextwritertag. Div );
}
Else
{
Writer. renderbegintag (htmltextwritertag. span );
}
Base. renderchildren (writer );
Writer. renderendtag ();
}
This. _ rendered = true;
}
In this method, you can see that pagerequestmanager is called in Ajax asynchronous processing. encodestring (writer, "updatepanel", this. clientid, writer2.innerwriter. tostring (); method, and then find the pagerequestmanager class. You can find that this class contains the onpageerror (Object sender, eventargs e) event processor, in addition, the onpageerror event processor is registered in the oninit initialization event processor of the pagerequestmanager class. It can be seen that when the control on updatepanel encounters an exception, updatepanel itself will process it, you cannot capture application_error.
Oninit method of pagerequestmanager class:
Internal void oninit ()
{
If (this. _ owner. enablepartialrendering &&! This. _ owner. _ supportspartialrenderingsetbyuser)
{
Ihttpbrowsercapabilities browser = This. _ owner. ipage. Request. browser;
Bool flag = (browser. w3cdomversion> = minimumw3cdomversion) & (browser. ecmascriptversion> = minimumecmascriptversion) & browser. supportscallback;
If (FLAG)
{
Flag =! This. enablelegacyrendering;
}
This. _ owner. suppsparspartialrendering = flag;
}
If (this. _ owner. isinasyncpostback)
{
This. _ owner. ipage. Error + = new eventhandler (this. onpageerror );
}
}
Onpageerror method of pagerequestmanager class:
Private void onpageerror (Object sender, eventargs E)
{
Exception lasterror = This. _ owner. ipage. server. getlasterror ();
This. _ owner. onasyncpostbackerror (New asyncpostbackerroreventargs (lasterror ));
String asyncpostbackerrormessage = This. _ owner. asyncpostbackerrormessage;
If (string. isnullorempty (asyncpostbackerrormessage )&&! This. _ owner. Control. Context. iscustomerrorenabled)
{
Asyncpostbackerrormessage = lasterror. message;
}
Int httpcode = gethttpcodeforexception (lasterror );
Bool flag = false;
If (this. _ owner. allowcustomerrorsredirect & this. _ owner. Control. Context. iscustomerrorenabled)
{
If (! This. customerrorssectionhasredirect (httpcode ))
{
Flag = true;
}
}
Else
{
Flag = true;
}
If (FLAG)
{
This. _ owner. ipage. response. Clear ();
Encodestring (this. _ owner. ipage. response. Output, "error", httpcode. tostring (cultureinfo. invariantculture), asyncpostbackerrormessage );
This. _ owner. ipage. response. End ();
}
}
I hope the above explanation can help you.
Jasson Wang
Online Technical Support Engineer
Microsoft Global Technical Support Center