To some the title of this post may be a no-brainer. But, until this afternoon I thought that AppDomain.CurrentDomain.UnhandledException was some sort of "global" exception handler (iow, a giant try ... catch around everything). Hook up a method, log the exception and you the coder decide whether to let the app continue on it's merry way. And never, ever see that standard .NET crash dialog (after all if I've "handled" the exception why is CLR stepping in?). Only it wasn't working that way at all. No matter what I did I was still seeing the crash dialog. (And some other weird behavior I can only attribute to the fact this was a Windows Service). I googled and googled and couldn't find anything. Then finally I found
I wasn't alone. Thankfully, the final comment on that post by Jonathan Keljo (CLR Exception PM), kicked the lightbulb on.
In part Jonathan explained:
The first thing to understand is that the UnhandledException event is not an unhandled exception "handler". Registering for the event, contrary to what the documentation says :-(, does not cause unhandled exceptions to be handled. (Since then they wouldn't be unhandled, but I'll stop with the circular reasoning already...) The UnhandledException event simply notifies you that an exception has gone unhandled, in case you want to try to save state before your thread or application dies.
So it's just an event handler, giving you one last chance to clean up or log, or whatever before the CLR handler kicks in. (Be sure to read the rest of his comments and
this article to understand how unhandled exceptions are "handled" by the CLR for various thread types. In some cases your app won't die at all in v1.0 and v1.1.)
Oh, and btw, if anyone has links to some decent articles on "hardening" a Windows Service please comment. At this point all I can come up with is a try catch in the .ctor, and the OnStart, OnStop, etc. methods. But, other than logging can't figure out what I should be doing in the catch block.