1

.Net Framework 4.6.1

Why the AppDomain.UnhandledException event doesn't happen in my case?

class MyClass {

    static void Main(string[] args) {

        AppDomain.CurrentDomain.UnhandledException +=
            UnhandledExceptionHandling;

        int a = 2;
        int b = 0;

        // Here I expected a jump to 
        // UnhandledExceptionHandling...
        // But I get the DivideByZeroException exception
        // instead of.
        int c = a / b; 
    }

    private static void UnhandledExceptionHandling(
        Object sender, UnhandledExceptionEventArgs e) {

        // But I don't get here...
        Console.WriteLine(((Exception)
            e.ExceptionObject).Message);
    }
}
Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • This works for me, just run the generated executable from the command prompt outside Visual Studio. – Darin Dimitrov Jan 08 '17 at 11:07
  • The debugger always automatically breaks when it detects an unhandled exception. Programs would be pretty hard to debug if it did not do that. Press Ctrl+F5 instead. – Hans Passant Jan 08 '17 at 13:02

0 Answers0