locked
How to Catch All Exceptions? RRS feed

  • Question

  • I wish to catch all exceptions on runtime, not debug time. You know, like when user face any issue, I can catch it and display it on screen for bug reporting. I am looking for a global catcher. Instead of catching few codes in a try+catch block, I catch eveything in my application.

    How do I do that? Thank you for the help.
    Tuesday, February 17, 2009 7:34 PM

Answers

  • Write an event handler for the AppDomain.UnhandledException event.  If you use Windows Forms, you'll also need one for Application.ThreadException.  I think WPF uses Dispatcher.UnhandledException, not that sure.  You cannot trap SEH exceptions that are raised in a thread that was started by unmanaged code.

    When you catch these exceptions, be sure to terminate your app.

    Hans Passant.
    • Marked as answer by Reza J Wednesday, February 18, 2009 2:16 PM
    Tuesday, February 17, 2009 10:42 PM

All replies

  • Put one Try - Catch around your entire application and rethrow all your other Catches.

    Tuesday, February 17, 2009 9:27 PM
  • Write an event handler for the AppDomain.UnhandledException event.  If you use Windows Forms, you'll also need one for Application.ThreadException.  I think WPF uses Dispatcher.UnhandledException, not that sure.  You cannot trap SEH exceptions that are raised in a thread that was started by unmanaged code.

    When you catch these exceptions, be sure to terminate your app.

    Hans Passant.
    • Marked as answer by Reza J Wednesday, February 18, 2009 2:16 PM
    Tuesday, February 17, 2009 10:42 PM
  • There are three weird exceptions which require additional knowledge/usage when attempting to handle them via try/catch.

    The three are ThreadAbortException, OutOfMemoryException and StackOverflowException.

    Wednesday, February 18, 2009 8:49 PM
  • Thank you nobugz, this is what I am looking for. I am using Try-Catch, but, that requires me to know where the exception may thrown. Using your approach is the right way to catch unexpected exceptions.
    Wednesday, February 18, 2009 10:54 PM