locked
Global Exception Handler RRS feed

  • Question

  •  

    Hello

     

    We are trying to create a "global" exception handler for those Exceptions that might be thrown and that we havent caught. We have try/catch blocks in our application but some "new" exceptions might occur. We tried this (see sample below) and so far these events seem to be "dead". Does anybody is familiar to this? One other thing. We "believe" that MAYBE these events won't work under Debug Mode. When we switched to Release, we got these errors related to missing DLL in the /bin/Release folder. Does anyone knows, other than copy and paste, what to do to copy all the DLL in our /bin/Debug folder?

     

    Thank you

     

    static class Program

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    //AddGlobalHandlers();

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    //Application.Run(new MainForm());

    Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    Application.Run(new MainForm());

    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

    {

    MessageBox.Show("CurrentDomain_UnhandledException: " + e.ExceptionObject.ToString());

    }

    static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)

    {

    MessageBox.Show("Application_ThreadException: " + e.Exception.Message);

    }

    }

    Thursday, April 17, 2008 3:58 PM

Answers

All replies

  •  

    you could just be naughty and wrap Application.Run in try/catch block, catching all exceptions.

     

    Thursday, April 17, 2008 4:02 PM
  • In the references folder in your application, make sure you've set the "Copy Local" property of each reference to "True" with the exception of those in the GAC.

     

    Also, why don't you try just doing something like:

     

    Code Snippet

    [STAThread]

    static void Main()

    {

    try {

    // execute your app here

    } catch (Exception ex) {

    HandleException(ex); // some kind of handler.

    }

    }

     

    I think you might find that a bit simpler and more straightforward.

     

    Of course, that would make all the exceptions that are uncaught bubble up the call stack, but then again, isn't that the point?  Smile

     

    Good luck.

    Thursday, April 17, 2008 4:03 PM
  • Note that you'll only see this error message if you run the app outside the debugger. Why? Because WinForms is making use of its own unhandled exception mechanism, and the default behavior differs based on whether you're debugging or not. This is slightly confusing, but worse it means that in some scenarios, your carefully-crafted catch block will never run.

    Thursday, April 17, 2008 4:12 PM
  • Can you give me an example of what you're talking about? 

     

    I just threw a DivideByZeroException in a form, and it caught and handled the error just fine when I was running in the debugger.

    Thursday, April 17, 2008 4:21 PM
  •  

    Ok. I just create a new whole project. i just added a button, then a method that would throw an ArgumentOutOfRange exception. The button activates the method... nothing happened. I added the same line of codes like i had before... using event subscription. i am gonna try your variant using the .Run inside a try... although, being honest... i don't like it too much. I kinda see it unstable. Let's see... i will comment back.

     

    Thank You

    Thursday, April 17, 2008 6:45 PM
  • Nothing...

    i just tried and nothing seems to be happening. I just did what you suggested me...

     

    is there anybody with a code sample that i could test in my computer?

     

    Thank you

     

    Thursday, April 17, 2008 7:12 PM
  • Hi

    For the Application.ThreadException Event usage, perhaps you can try to view the following MSDN article.

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.threadexception.aspx

    For using exception in .Net, please check the LINK below.

    http://msdn2.microsoft.com/en-us/library/ms173161.aspx

    Regards,

    Xun

     

    Monday, April 21, 2008 9:16 AM
  • <<Of course, that would make all the exceptions that are uncaught bubble up the call stack>>

    There you go getting salty again.

    Tuesday, February 7, 2012 3:24 PM