Answering follow-up Question in response to the Answer by Ryan:
Exception is not catched caught in catch block.It simply means one of the two: 1) you report is incorrect; exception is thrown elsewhere, 2) exception is caught but you failed to detect it because wrong exception handler, for example; simply use debugger.
Assuming you would not miss caught exception (case 2); but exception is thrown elsewhere, here what to do:
Catch exception at the very top of the stack of for each thread. Make sure you never block exception from propagation anywhere in the middle; if you catch any exception, always re-throw it in the same block or throw another exception (except the very top where you show exception somehow, never hide it completely). There are very few exclusions from this rule, usually as a last resort in case of some ill-designed 3-rd party libraries.
To get full exception information and locate to source of it, make full exception dump in your top-level exception handler: include exception
Stack
(most important) and collect exception information on all inner exceptions, recursively (use Exception.
InnerException
). You can save dump in some file or
System.Diagnostics.EventLog
. By the way, this is the information you may need to use in your CodeProject Questions.
For
System.Windows.Forms.Application
, "very top" is in the main UI cycle.
This is how: add a handler to
Application.OnThreadException
(show exception info in UI); to make this event firing, assign
Application.SetUnhandledExceptionMode
to
UnhandledExceptionMode.CatchException
.
Attention! It won't happen by default!
For WPF, there is a similar technique. You need to handle the event
Application.DispatcherUnhandledException
.
[EDIT]
Additionally or alternatively, handle the events
System.AppDomain.UnhandledException
and
System.AppDomain.FirstChanceException
.
Credit to Espen Harlinn for the idea.
See also:
How do i make a loop that will stop when a scrollbar reaches the bottom[
^]
[END EDIT]
—SA