Tricky exception trapping question

J

JWA

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
 
D

David Levine

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.

JWA said:
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?
 
A

Alan Pretre

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.


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

-- Alan
 
D

David Levine

Alan Pretre said:
That's not always possible if a control throws an exception.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top