views:

19

answers:

1

I've looked into this quite extensively and I've found the following:

  1. You can do some clever stuff to catch most errors by implementing a base class, see Andreas Knudsen's solution for this.
  2. The Error event in UserControl never gets fired, see details here: http://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx

What I can't find is any general way to catch errors occurring in postback events, such as the click event for a button, at the web part or user control level. What I mean by general is a something I can implement in a baseclass.

I'm aware the I should do proper try/catch in my code, but for large teams I'd like to be sure that a web part never crashes the page, but always shows a nice message and allows execution to continue for the other web parts on the page.

I don't think it's possible but I'd love to be proved wrong.

Thanks, Bjoern

A: 

I think this is possible, but you have to work around the fact that when asp.net distributes the postback events it couldn't care less about your user control definitions, instead it only cares about controls that explicitly implement IPostBackEventHandler (like button / dropdown etc)

For pages you can override and try/catch RaisedPostBackEvent like for the other methods. (if you ever wanted a generic page-exception handling setup. (if you want this then please reconsider. I spent way too much time in my last project basically reimplementing the default asp.net exception handling logic just to get this to work. the devil's in the details)

What you could do is to have a base page in your system which all pages inherit from and which overrides RaisePostBackEvent(source, eventArgs). In this method you could see if the source inherits from your exception handling base control, or if it is contained within a control which does this. (navigate the parent graph) If it is contained by one then do a try/catch around the call to base.Raise.... (see the code in http://stackoverflow.com/questions/280412/transparent-generic-exception-handling-for-asp-net-moss2007-with-code/3698701#3698701 ) and call the exceptionhappened method on the first candidate you found if indeed any exceptions occur.

AndreasKnudsen
Thanks for the answer. It's an interesting idea, but unfortunately I don't have time at the moment to try it out (project pressures). I will some day though as it would be very cool if it worked.
Bjoern
I'll give you the benefit of the doubt :)
Bjoern