Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

clr

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?

like image 504
chillitom Avatar asked Jun 11 '12 14:06

chillitom


2 Answers

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.

like image 80
Hans Passant Avatar answered Oct 29 '22 21:10

Hans Passant


No, unhandled exceptions from managed threads spawned by another application will have IsTerminating equal to false, since it'll kill the thread but not the overall application. That's the only time I've ever seen IsTerminating false in this situation, and the MSDN page says that this is the main use-case too.

like image 23
tmesser Avatar answered Oct 29 '22 22:10

tmesser