Unhandled Exception handler

C

Chris Dunaway

I have a Windows forms app which is started from a sub Main. In Sub
main I have this line:

AddHandler System.AppDomain.CurrentDomain.UnhandledException, AddressOf
Form1.UnhandledExceptionHandler

And I also have the method, UnhandledExceptionHandler with the proper
signature. The handler logs details of the exception to a log file as
well as sending an EMail. When I run the program through the IDE,
exceptions are caught by this handler and the appropriate logs and
emails are successfully sent.

If I run the exe directly, outside of the IDE, I get a MsgBox that says
"An unhandled exception has occurred in your application....." with a
Details, Continue and Quit buttons. When I click the Details button I
see the exception message and stack trace but for some reason my
handler does not get called. I have even attached to the process with
the debugger and set a breakpoint but it never gets hit.

Why does the code catch the error in the IDE but not when run outside
the IDE? Someone suggested to me that there was bug introduced with
version 1.1 of the framework. Does anyone have any information on
this?

Thanks,

Chris
 
C

Chris Dunaway

To provide an example that illustrates the problem I created a Windows
Forms app with a single button. I added the following two methods to
the form class:

Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal
e As UnhandledExceptionEventArgs)
Dim ex As Exception = DirectCast(e.ExceptionObject, Exception)
MsgBox("An Unhandled Exception has occurred: " & ex.Message)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Throw New ApplicationException("This exception is unhandled")
End Sub

I added the following statement to the constructor:

AddHandler AppDomain.CurrentDomain.UnhandledException,
AddressOf UnhandledExceptionHandler

When I run this program in the IDE and click the button, I get the
MsgBox as I expect. If I compile the app and run the .exe through
explorer and click the button, I get a notification from Windows about
the exception but my exception handler *is not called*.

Can anyone shed any light on this?
 

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