Search code examples
c#.netclr

What causes the UnhandledExceptionEventArgs.IsTerminating flag to be true or false?


When subscribed to events on AppDomain.CurrentDomain.UnhandledException what criteria causes the UnhandledExceptionEventArgs IsTerminating flag to be true? I.e. what causes an exception to be considered fatal?

Is it the case that by default all unhandled exceptions will be fatal unless configured otherwise?


Solution

  • This property is always true. It used to be possible to be false, way back in the .NET 1.x days. That version allowed a thread to die on an unhandled exception without that having the entire process terminate. That just didn't work out well, programmers didn't implement the event handler (or didn't know how to properly handle the exception from the event, who does) so threads just died without any notice whatsoever. Almost impossible to not have this cause difficult to diagnose program failure.

    Microsoft changed the default behavior in .NET 2.0, an unhandled exception terminates the program. Technically it is still possible to override this behavior, a custom CLR host can keep the process alive by implementing the IHostPolicyManager interface. And the default host supports the <legacyUnhandledExceptionPolicy> config element. Don't use it, that way lies dragons.