24

A C# program is invoked by:

Application.Run (new formClass ());

I'd like to put a try/catch around the whole thing to trap any uncaught exceptions. When I put it around this Run method, exceptions are not caught; control only returns here when the program terminates after an uncaught exception.

Where can I put try/catch to cover the whole program? Thanks!

Graviton
  • 81,782
  • 146
  • 424
  • 602
user20493
  • 5,704
  • 7
  • 34
  • 31
  • 4
    I think you are missing the point of the exception process, if an exception bubbles up to the top-most layer of your application then there's something wrong that needs addressing lower down in the code. Put try/catch around code you think might fail and provide mitigation (where possible) in the catch. If it can't be mitigated, i.e. the app can't recover, then the application needs to fail. – Lazarus Oct 21 '09 at 14:56
  • 13
    The OP is not asking how exceptions should be used - just how to catch them in a certain scenario. Obviously if an exception bubbles to the top layer then something is wrong - seems like he wants to find out more information when something is wrong so it can be addressed. Mitch's answer is much more helpful than assuming the op doesn't know what he's doing. – Maggie Oct 21 '09 at 15:05
  • 10
    Catching all exceptions at the top level can be very useful for making a final effort to report what happened before exiting. In many cases I think this is better then seeing the standard Windows "this program has crashed, please report this to Microsoft" dialog. – noctonura Oct 21 '09 at 15:12

2 Answers2

41

To catch Windows Form's unhandled exceptions hook-up the AppDomain.UnhandledException and Application.ThreadException events.

Of interest: Unexpected Errors in Managed Applications

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 3
    @alankdkd: Make sure you read to the end and the "Some Best Practices" section ;) – Lazarus Oct 21 '09 at 15:09
  • 1
    You cannot catch an exception with `AppDomain.UnhandledException` (it will still terminate the application), but rather just get notified. This is an important difference compared to `Application.ThreadException` which is an exception catch. – Lucero Oct 21 '09 at 15:13
8

Basically, you cannot catch all exceptions when using the default CLR hosting process. Period. This is because the AppDomain.UnhandledException event is a notification only, you cannot handle the exception (which means that you cannot prevent the application from being terminated after processing the notification).

However, you can catch and handle all exceptions in the UI thread of a WinForms application by using its Application.ThreadException handler (and control the behavior via UnhandledExceptionMode). Other threads which throw an exception will not be caught by this handler.

In general, it's not a good idea to try and handle all exceptions. You can, however, use the AppDomain.UnhandledException to log the error and/or perform important cleanup tasks (e.g. shutting down a file-based databaseor whatever).

Lucero
  • 59,176
  • 9
  • 122
  • 152