views:

1384

answers:

3

I have simple application with single AppDomain which is periodicaly launched on a server. Sometimes unhandled exception occurs in the aplication and default abort/retry/ignore dialog pops up. I need to somehow prevent the edialog from showing and just output the exception on StrErr and close the application. So I enclosed all the code in main method with try-catch statement, but it didn't help at all - the exception dialog is still shown sometimes.

The Main() code looks like this:

try
{
    RunApplication();
}
catch (Exception exc)
{   
    Console.Error.WriteLine(exc.ToString());
    Console.Error.WriteLine(exc.StackTrace);
    if (exc.InnerException != null)
    {
       Console.Error.WriteLine(exc.InnerException.ToString());
       Console.Error.WriteLine(exc.InnerException.StackTrace);
    }
    Environment.Exit(666);
}

This try-catch clause shoud catch all unhandled exceptions and the exception dialog should never popup AFAIK. Am I missing something? Or is there any setting (registry etc) on the server which controls some special behaviour related to the exception dialog/application error code?

+9  A: 

There's an unhandled exception event you can subscribe to in the application domain.

    public static void Main()   
    {   
        AppDomain.CurrentDomain.UnhandledException +=   
            new UnhandledExceptionEventHandler(   
                OnUnhandledException);

        //some code here....
    }   

    /// <summary>
    /// Occurs when you have an unhandled exception
    /// </summary>
    public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
    { 
        //here's how you get the exception  
        Exception exception = (Exception)e.ExceptionObject;  

        //bail out in a tidy way and perform your logging
    }
DoctaJonez
Damn, too slow. ;-)
Konrad Rudolph
I was typing furiously, lol :-)
DoctaJonez
I havn't done any work with appdomains before, I just know my applications execute inside one... Would this code catch exceptions that fire from different threads?
Matthew Scharley
this does not prevent the exception dialog from showing as far as I know - it just enables to report the exception, but the exception can not be handled in this event.http://www.codinghorror.com/blog/archives/000201.html
Buthrakaur
anyway the try-catch encapsulation of Main method body should work if the application does not have more threads or AppDomains.
Buthrakaur
The event will fire for any exceptions on any thread (see below msdn excerpt)If the UnhandledException event is handled in the default application domain, it is raised there for any unhandled exception in any thread, no matter what application domain the thread started in.
DoctaJonez
MSDN: This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspxSo the default handler is always called - this event does not help in this case.
Buthrakaur
It will not call the default exception handler if you call Environment.Exit() before the end of the that event handler.
Matthew Scharley
ie. Throw your current catch statement into the event handler and you should be good to go.
Matthew Scharley
I see.. I can try it of course, but what is the exact difference between using this event + Environment.Exit and try-catch in Main() which I use now? It should behave in the same way both when I have single thread and single AppDomain. What's the advantage of the event over try-catch in Main?
Buthrakaur
Some Exceptions can't and/or shouldn't be caught. As I understand it, this event should be fired just before the default windows exception handler which asks about debugging, for any exception. Which means you need to be careful about what you try to do, but no more so than when you catch all Exception's directly I suppose.
Matthew Scharley
A: 

Have you considered the possibility that your catch clause may be throwing exceptions? Do you spawn threads in your main app?

Tormod
there're no other threads - just the main thread. The only possibility the catch clause would raise exception I can see now is when exc.StackTrace == null.
Buthrakaur
A: 

What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException ?

Also for me, the eventhandler of Application.ThreadException gets called instead of the eventhandler of AppDomain.CurrentDomain.UnhandledException when both of them are defined