I've hooked both of these, but occasionally my app still crashes and just throws up the Windows "an exception开发者_高级运维 has occurred" dialog. When I click OK, the app just exits. Under what circumstances won't exceptions get caught with these two handlers (in a WPF app)?
I should mention that I'm doing a bunch of COM interop, so that's always suspect :-)
There are a couple of reasons why an unhandled exception would not be raised by either of these.
The Dispatcher.UnhandledException
is only raised for exceptions that occur in the look of the dispatcher where the method was entered via an Invoke or BeginInvoke call (documentation). So exceptions on other threads or methods that were not invoked in this way will miss this event handler.
.Net also made a change in 4.0 to what exceptions will go into the AppDomain.UnhandledException
event. Corrupted state exceptions such as access violations and stack overflows no longer go through this event.
I'm not sure if either of these will be relevant to you. The simplest way to check is to just attach a debugger and see what output it produces.
There are uncatchable exceptions in .NET. These are exceptions thrown directly by the CLR when it gets into a state that it considers to be completely unrecoverable. One example is the StackOverflowException. Microsoft's philosophy is that this state isn't a possible exceptional circumstance, it must be a bug in your code and therefore shouldn't be handled. In this case the exception will immediately propagate right the way up to a application closing error message.
精彩评论