views:

168

answers:

2

Or any other design that has the gui widgets as being the first port of call where an unhandled exception will kill the app?

We all want one main: "catch all" for face saving purposes (although in most cases this shouldn't have a "continue" feature) but it is impossible to easily implement one with gui widgets that are the first port of call. Is this design flawed or am I missing a trick somewhere?

I ask this because I was reading an article by the designer of the language and he stated that Exception handling should be centralised and the first thing that popped into my head was the WinForm event mechanism where this becomes difficult.

Were the WinForm/GUI team listening to him, or was it possibly too late by then?

+1  A: 

Are you aware of Application.ThreadException? I'm not saying it's necessarily the best answer around, but it's at least an answer...

Jon Skeet
I thought this was only for background threads as opposed to main UI thread? I remember there used to be AppDomain.UnhandledException but that was changed as per 2.00 to no longer handle only give you a chance to log. It also isn't supported (ThreadException) in Compact Framework. :(
Quibblesome
No, Application.ThreadException is explicitly for UI threads. However, if you need Compact Framework support you're out of luck :(
Jon Skeet
+2  A: 

You can put a try/catch around Application.Run, and handle Application.ThreadException. In the handlers you can log the exception and exit.

Apart from the above, I usually put an exception handler around all calls to the Business Tier (and all access to external resources in the client tier, e.g. export to a file) which display an error but don't exit the application.

Joe