I am trying to attach a unhandled exception handler for .NET excel addin. The problem is the exception handler is never called.
AppDomain.CurrentDomain.UnhandledException doesn't work - never fires the event.开发者_如何学编程
Application.Current.DispatcherUnhandledException can't be used as Application is null in the excel addin code.
how do you wire an unhandled exception handler then?
You might not be able to, since the application is actually an unmanaged application. You might have to just put try/catch blocks around flaky areas.
From a user defined function in your XLL you have to trap the exceptions. You should be using a catch handler to catch everything. It's a code smell but make sure any logging or other processing of the error within the catch doesn't throw by wrapping that with an empty catch handler.
Same from the UI thread for any ribbon event handlers.
If you create your own window any exceptions you leak there will crash excel. If you're using a WPF window then you can set up a global handler off the dispatcher during the connection of the addin.
Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_UnhandledException;
Excel is finicky at best so treat her like dottie
精彩评论