Is there a way that I can catch any exception that is not handled further down the stack? Basically if an exception occurs and is not handled then I want a catch all method to log the exception to a file along with some other information. Thoughts?
blah blah blah is here! blah blah » Close
1 answers
.NET has some options for handling (otherwise unhandled) exceptions, globally:
1. In a Windows Forms application, you can use Application.ThreadException to catch exceptions on the main UI thread.
2. In a WPF application, you can use Application.DispatcherUnhandledException to catch exceptions on the main UI thread.
3. In any type of application, you can can use AppDomain.UnhandledException as the handler of last resort though there's no way to stop the application from closing afterwards.
In the first two cases you need to handle exceptions which occur on worker threads yourself (unless you're using a BackgroundWorker or asynchronous delegate) or rely on #3.
However, in WPF there is a way to dispatch exceptions on worker threads back to the main UI thread where they can be rethrown and caught (see here for details).
answered one year ago by:
17279