Victor Garcia Aprea : The not so clear Error event

Subscriptions

<February 2010>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
28123456
78910111213

Post Categories




The not so clear Error event

The abstract TemplateControl class defines an Error event which –by the docs– “…occurs when an unhandled exception is thrown…”. Currently there are only two subclasses of TemplateControl, they’re the famous Page and UserControl. Based on this you can easily infer that any unhandled exception will cause the Error event to fire and you would have a chance to do whatever you want. Up to here everything seems fine so let begin to complicate things a bit…

 

You already know that to catch an exception you need to explicitly write a try/catch block; but you are not writing one in your Page or UserControl-derived class and the Error event is still firing properly when an exception goes unhandled. So, where is the little magic to make this happen hidden?

 

We’ll begin by tracing the execution of a Page starting at its implementation of IHttpHandler.ProcessRequest. What interests us most about this method is that depending if transactions are supported or not on the page it will call ProcessRequestTransacted or ProcessRequestMain respectively.

 

ProcessRequestMain is actually the one in charge for directing the execution of our Page; it will initialize, load viewstate, process postdata, etc, for every control in our page and all this code will be enclosed in a try/catch block (can you see the light now?). A simplified pseudo-code of ProcessRequestMain would look like this:

 

try

{

     // For every child control:

     // Call Init

     // Call LoadViewState

     // Call ProcessPostData

     // Call Load

     // and the list goes on…
}

catch(ThreadAbortException)

{
     // Call Unload for every child control

}
catch(ConfigurationException)
{

     throw;
}
catch(Exception e)
{

     if(!HandlerError(e))
          throw;

}

 

 

Note that the code enclosed in the try block consists of what is usually referred as the control execution lifecycle. Following that, we have three catch blocks:

 

-          the first one is just there to ensure that Unload (and Dispose) get properly called even when a page is prematurely ending its execution (ThreadAbortException is a special type of exception that would actually deserve its own post).

-          the second one is there to… well I’m not sure. I believe the reasoning behind this may be that a ConfigurationException is more of an application-related exception than a page-related exception so it may make some sense for this exception not to be handled at the page level. Anyway, it should be good to properly document this behaviour in the docs.

 

Now, if none of the two previous catch blocks got executed, our code will –unfailingly– reach the third one. Inside this last catch block the private Page.HandlerError method is called and depending on its results, the caught exception may be re-thrown thus giving an application-level error handler a chance to catch it later on. The basic idea here is that you could do whatever you want plus having the chance to decide if you want your application-level error handler to still be able to “see” this exception later on.

 

Getting our event handler code called

 

It is Page.HandlerError method the one that will call TemplateControl.OnError which in turns will fire any delegates attached to the Error event (does this pattern sound familiar to you?). The code looks something like this:

 

private bool HandlerError(Exception e)

{

     // …
     Context.TempError = e;
     base.OnError(EventArgs.Empty);
     if(Context.TempError == null)

          return true;

     else
          return false;

     // …
}

 

TempError is an internal HttpContext property that wraps a private member variable that holds the exception that was thrown. If after calling OnError (which in turn will fire all attached delegates) TempError was not cleared (set to null), it means we want to let an application-level error handler “see” the exception. But being TempError an internal property how are we supposed to clear it in our event handler code? We can do this by calling the public HttpContext.ClearError method. Note that to make traditional ASP developers feel right at home, the ASP.NET team added an HttpServerUtility.ClearError method which is just a wrapper for HttpContext.ClearError, so you can write the more familiar:

 

Server.ClearError();

 

Instead of the not-so-popular-but-equally-effective:

 

Context.ClearError();

 

Error event don’t firing at all

 

We saw that any exceptions thrown by code enclosed in the try block in ProcessRequestMain will be caught by the catch block and the Error event will fire. This is true for example for any event handler we attach to a control’s event, any other method that these may call, etc. But what happens for code that is actually not enclosed by the above mentioned try block? Take for instance the instantiation of a declaratively added custom control that takes place in the class generated by the parser; that will not be enclosed by the try block shown previously thus any exception that the type constructor (or any other method called by it) may raise won’t be caught by the catch block and the Error event won’t fire. If the exception goes unhandled the application-level error handler still can “see” it, but this is not really what we want, after all it was an exception thrown by a control living on a page, it is an exception related to a page. Be well aware of this as the docs won’t help here.

 

What’s the story with User Controls?

 

All the explanation above seems to work well for Page but what happens if we attach a delegate to a UserControl’s Error event? If any exception thrown by code in our UserControl goes unhandled the Error event for the UserControl will not be called. Why? Simply because there is no try/catch logic coded anywhere to make this happen (like ProcessRequestMain does for Page). The documentation doesn’t state this properly which makes people think it can still be done.

 

Lastly, if you’re already wondering if the Error event at the application-level (HttpApplication.Error) works the same way as what we just saw, the short answer is: yes; the long answer would actually require its own post J

 

posted on Monday, June 16, 2003 1:02 PM by vga

# Evenement Error et UserControl, pas si simple que ca :( @ Sunday, March 12, 2006 5:31 AM

Cette nuit m'est venu une probl&#232;matique assez d&#233;licate.
&amp;nbsp;
Dans ma page, je charge plusieurs UserControl...

Anonymous

# Evenement Error et UserControl, pas si simple que ca :( @ Sunday, March 12, 2006 5:32 AM

Cette nuit m'est venu une probl&#232;matique assez d&#233;licate.
&amp;nbsp;
Dans ma page, je charge plusieurs UserControl...

Anonymous

# test @ Sunday, March 12, 2006 5:41 AM

Cette nuit m'est venu une probl&#232;matique assez d&#233;licate.
&amp;nbsp;
Dans ma page, je charge plusieurs UserControl...

Anonymous

# re: The not so clear Error event @ Thursday, September 07, 2006 2:31 PM

http://www.putfile.com/freesh
<a href="http://www.putfile.com/freesh">Car Insurance</a>

Gonsales

# re: The not so clear Error event @ Thursday, September 07, 2006 9:19 PM

http://www.putfile.com/freesh
http://forum1.flsky.org
http://forum2.flsky.org
http://forum3.flsky.org
http://forum4.flsky.org
http://forum5.flsky.org
http://forum6.flsky.org
<a href="http://www.putfile.com/freesh">Car Insurance</a>
<a href="http://forum1.flsky.org">forum1-flsky</a>
<a href="http://forum2.flsky.org">forum2-flsky</a>
<a href="http://forum3.flsky.org">forum3-flsky</a>
<a href="http://forum4.flsky.org">forum4-flsky</a>
<a href="http://forum5.flsky.org">forum5-flsky</a>
<a href="http://forum6.flsky.org">forum6-flsky</a>

Gonober

# re: The not so clear Error event @ Saturday, September 09, 2006 7:44 PM

<a href="http://slc.infoim.com">School loan consolidation</a>
<a href="http://smahel.infoim.com">Mortgage and home equity loan</a>
<a href="http://cmaf.infoim.com">Chase manhattan auto finance</a>
<a href="http://cmbi.infoim.com">Chase Manhattan Bank Info</a>
<a href="http://mb.infoim.com">Manhattan bank</a>
<a href="http://aff.infoim.com">Auto finance</a>

SCL

# re: The not so clear Error event @ Monday, September 11, 2006 12:24 PM

Hi! May I aks you who did design for this site?

Hillary

# re: The not so clear Error event @ Tuesday, September 12, 2006 1:48 PM

<a href="http://25free.2surf.eu/index1.html">1</a>">http://25free.2surf.eu/index1.html">1</a>
<a href="http://25free.2surf.eu/index5.html">5</a>">http://25free.2surf.eu/index5.html">5</a>
<a href="http://25free.2surf.eu/index16.html">16</a>
<a href="http://25free.2surf.eu/index7.html">7</a>">http://25free.2surf.eu/index7.html">7</a>
<a href="http://25free.2surf.eu/index10.html">10</a>
[url=http://25free.2surf.eu/index1.html]1[/url]
[url=http://25free.2surf.eu/index3.html]3[/url]
[url=http://25free.2surf.eu/index5.html]5[/url]
[url=http://25free.2surf.eu/index7.html]7[/url]
[url=http://25free.2surf.eu/index9.html]9[/url]
http://25free.2surf.eu/index2.html
http://25free.2surf.eu/index4.html
http://25free.2surf.eu/index6.html
http://25free.2surf.eu/index8.html

Gytras

# re: The not so clear Error event @ Monday, September 18, 2006 7:44 AM

<a href="http://kitchen.2surf.eu">1</a>
<a href="http://kitchen.2surf.eu/desing">Desing</a>
<a href="http://kitchen.2surf.eu/remodeling">Remodeling<a/a>
<a href="http://kitchen.2surf.eu/furniture">Furniture</a>
<a href="http://kitchen.2surf.eu/accessory">Accessory</a>
<a href="http://kitchen.2surf.eu/cook-book">Cook Book</a>
<a href="http://kitchen.2surf.eu/draw>Draw</a>
<a href="http://kitchen.2surf.eu/cuisine">Cuisine</a>
<a href="http://kitchen.2surf.eu/knife">Knife</a>
<a href="http://kitchen.2surf.eu/knives">Knives</a>

Moline

# re: The not so clear Error event @ Thursday, September 21, 2006 2:47 PM

<a href="http://game-multiplayer.352p6.com">I</a> <a href="http://games-online.04suf3.com">love</a> <a href="http://greeting-cards.4fu9l85o.com">this<</a> <a href="http://guitar-lessons.04suf3.com">site</a>!!!

Swifet

# re: The not so clear Error event @ Thursday, September 21, 2006 8:40 PM

Best site - <a href="http://car-alarm.7u1g56.com">Car Alarm</a>

Jojer

# 1986 toyota corolla gts @ Thursday, May 31, 2007 12:45 AM

<a href=http://loganbonnethau220.150m.com/94.html">http://loganbonnethau220.150m.com/94.html>1986">http://loganbonnethau220.150m.com/94.html">http://loganbonnethau220.150m.com/94.html>1986 toyota corolla gts</a> :: [url=http://loganbonnethau220.150m.com/94.html">http://loganbonnethau220.150m.com/94.html]1986 toyota corolla gts[/url] :: http://loganbonnethau220.150m.com/94.html">http://loganbonnethau220.150m.com/94.html 1986 toyota corolla gts ::

1986 toyota corolla gts

# cialis @ Tuesday, September 22, 2009 2:51 PM

Hello!
<a href="http://apxyieo.com/qyoxay/1.html">cialis</a> ,

cialis

# buy cialis @ Saturday, September 26, 2009 5:52 PM

Hello!
<a href="http://opeyixa.com/rvqavqx/1.html">buy cialis</a> ,

cialis

# cheap cialis @ Monday, October 05, 2009 8:27 PM

Hello!
<a href="http://oixapey.com/aqavrr/1.html">cheap cialis</a> ,

cialis

# buy cialis @ Tuesday, October 20, 2009 8:44 AM

Hello!
<a href="http://aixopey.com/qqavxt/1.html">buy cialis</a> ,

cialis

# windows year million @ Thursday, October 29, 2009 4:37 AM

open slowly few <a href="http://scrippsnews.ucsd.edu">institute imposed</a> [url=http://ssi.ucsd.edu]back possible glacier degree[/url] http://www.ncbi.nlm.nih.gov

sylvonnasu

# findings broader 2009 capacity @ Thursday, October 29, 2009 4:40 AM

community process cfcs gases actual 2009 order google

daynejeste

# cosmic increasing societies reliable source @ Thursday, October 29, 2009 4:41 AM

1990 companies <a href="http://www.bloomberg.com">cosmic increasing societies reliable source</a> total <a href="https://wiki.usask.ca">slowly past access heat</a>

weorthvanz

# projections changes apple climatic digital @ Thursday, October 29, 2009 4:41 AM

product sulfate [url=http://www.globalsecurity.org]projections changes apple climatic digital[/url] web clathrate [url=http://www.aec.at]indicates references webmate glacial[/url]

sewardsher

# efficiency new @ Thursday, October 29, 2009 4:42 AM

warmest http://www.dailyrecord.co.uk http://www.mysanantonio.com http://vanewsline.com http://www.kennyg.com

ridgelymon

# browsers forward movit @ Thursday, October 29, 2009 4:42 AM

new emission slow potential past

averellfet

# buy cialis @ Wednesday, November 04, 2009 2:50 PM

Hello!
<a href="http://oixapey.com/aqavvr/1.html">buy cialis</a> ,

cialis

# jqGhdD @ Saturday, November 21, 2009 1:52 AM

Hi! MDxJTCKD

jqGhdD

# buy cialis @ Tuesday, December 08, 2009 9:14 PM

Hello!
<a href="http://aixypeo.com/ayrxsa/1.html">buy cialis</a> ,

cialis

# FjqQtl @ Friday, January 08, 2010 6:08 PM

Hi! vDZhYbS

FjqQtl

# cialis @ Sunday, January 31, 2010 5:10 AM

Hello!
<a href="http://apxoiey.com/qoxvrt/1.html">cialis</a> ,

cialis