Tricky exception trapping question

Tricky exception trapping question

Post by JWA » Fri, 13 Aug 2004 07:42:37


Hi All,

How can you capture unhandled exceptions thrown by an MDI child form separate from the entire app?

I have an application-wide exception handler to catch crashes by doing the following in my Application entry point:

Public Shared Sub Main()
'Explicitly set apartment state to Single Thread Apartment (STA)
System.Threading.Thread.CurrentThread.ApartmentState = System.Threading.ApartmentState.STA
Dim eh As New MyExceptionHandler()
AddHandler Application.ThreadException, AddressOf eh.OnThreadException
Application.Run(New Form1)
End Sub

The form that I'm using to handle this reports/logs the exception and closes down the application. This works perfectly. I now want to enhance this
functionality by catching unhandled exceptions within individual MDI Child forms so that I can close down just the child form and not the entire app.
I'm having trouble isolating the exceptions thrown by the child forms separately. Does anyone have any tips on how best to do this?

Thanks,

--Josh
 
 
 

Tricky exception trapping question

Post by David Levi » Fri, 13 Aug 2004 19:42:06

There's no mechanism that allows you to add an unhandled exception handler
on a per-mdi child basis. You should also be aware that the
Application.ThreadException event will not be generated if the exception
occurs on any other thread then the one the winform itself is executing on.

If you want to catch all exceptions that can occur in the app regardless of
the thread it occurs on you should subscribe to the
AppDomain.UnhandledException event.

I prefer to use try-catch statements to catch the exceptions as they occur.
This will allow you to catch exceptions on a per-mdi basis as it will catch
the exception immediately so you have the full context available to your
code.



separate from the entire app?
following in my Application entry point:
System.Threading.ApartmentState.STA
eh.OnThreadException
closes down the application. This works perfectly. I now want to enhance
this
forms so that I can close down just the child form and not the entire app.
separately. Does anyone have any tips on how best to do this?

 
 
 

Tricky exception trapping question

Post by Alan Pretr » Sat, 14 Aug 2004 00:55:27


occur.
catch


That's not always possible if a control throws an exception.

-- Alan
 
 
 

Tricky exception trapping question

Post by David Levi » Sat, 14 Aug 2004 12:14:39


It depends if you are wrapping the control's execution thread in its
entirety or if it throws the exception on another thread, or in a code path
that the try-catch does not cover. Obviously a great deal depends on how the
code is structured, and it is too complicated to fully describe in a single
email. What I said earlier in that exchange partially describes the problem
and a workaround. Subscribing to the Application.ThreadException will not
catch all exceptions, only those that occur on the thread the winform itself
is on. If a control starts another thread (e.g. a background worker thread)
and it throws an exception, it will not be seen by the
Application.ThreadException handler. Conversely, wrapping a single code-path
may not catch all the exceptions either.