none
Catching Exception at App Level

    Question

  • Hello,

    I want to log general exception (fatal exception).  I was wondering is there anyway for me to catch exceptions at my application level (the top most level, System.Windows.Application).

    I have a try catch block in my System.Windows.Application.App() [Constructor]:

    try

    {

             MainWindow mw = new MainWindow();

              mw.Show();

    }

    Catch(Exception e) {Log(e); Application.Current.Shutdown()}

    I can't believe that this is the right appraoch :(

    I guess I could write a console application and in the Main start up the app, that way I can catch all the exception.

    Thanks in advance

    Houman

    Sunday, September 17, 2006 3:58 AM

Answers

  • You can hook up to Application.Current.DispatcherUnhandledException event to catch all the unhandled exceptions, or you can use AppDomain.CurrentDomain.UnhandledException event.

    Sheva
    Sunday, September 17, 2006 4:09 AM

All replies

  • You can hook up to Application.Current.DispatcherUnhandledException event to catch all the unhandled exceptions, or you can use AppDomain.CurrentDomain.UnhandledException event.

    Sheva
    Sunday, September 17, 2006 4:09 AM
  • Thanks that's exactly what I wanted.

    Houman

    Monday, September 18, 2006 1:46 PM
  • Can you give an example of doing this ?

     

    thanks

    Friday, July 16, 2010 1:02 PM
  • A potentially more complete way to do it is to provide your own Main() function as an entry point and wrap it in a try / catch block.

    These links might help:

     

    http://www.infosysblogs.com/microsoft/2008/09/how_to_write_custom_main_metho.html

    http://www.wintellect.com/CS/blogs/jclark/archive/2005/03/30/simple-main.aspx

     

    The problem with using the DispatcherUnhandledException event is that it won't catch any exceptions that happen while creating the Application object.

    • Proposed as answer by dumbledad Thursday, February 09, 2012 4:11 PM
    Friday, July 16, 2010 1:29 PM