I was just working on fixing exception handling in a .NET 2.0 application, and I came across some weird issue with Application.ThreadException .
I want to catch all exceptions from events behind GUI elements (like button_Click, etc.). Then I want to filter out these exceptions for lethality, for example. with some types of exceptions, the application should continue to work, and with others, exit.
In another .NET 2.0 application, I found out that by default only in debug mode exceptions actually leave a call to Application.Run or Application.DoEvents. In release mode, this does not happen, and exceptions should be "caught" using the Application.ThreadException event.
Now, however, I noticed that the exception object thrown in the ThreadExceptionEventArgs event of the .ThreadException application is always the innermost exception in the exception chain . For logging / debugging / designing, I really want the whole chain of exceptions. It is not easy to determine which external system failed, for example, when you simply throw a SocketException: when it fails, for example, an NpgsqlException, then at least you know that this is a database problem.
So, how to get to the entire chain of exceptions from this event? Is this possible, or do I need to construct excepion handling differently?
Note that I have a -sort of-have workaround using Application.SetUnhandledExceptionMode , but this is far from ideal, because I have to roll my own message loop.
EDIT: to prevent more errors, the GetBaseException () method does NOT do what I want : it just returns the innermost exception, and the only thing I already have is the innermost exception. I want to get the most external exception!