Unhandled Exception handler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Dunaway

    #1

    Unhandled Exception handler

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

    AddHandler System.AppDomai n.CurrentDomain .UnhandledExcep tion, AddressOf
    Form1.Unhandled ExceptionHandle r

    And I also have the method, UnhandledExcept ionHandler 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

  • Chris Dunaway

    #2
    Re: Unhandled Exception handler

    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 UnhandledExcept ionHandler(ByVa l sender As Object, ByVal
    e As UnhandledExcept ionEventArgs)
    Dim ex As Exception = DirectCast(e.Ex ceptionObject, Exception)
    MsgBox("An Unhandled Exception has occurred: " & ex.Message)
    End Sub

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    Throw New ApplicationExce ption("This exception is unhandled")
    End Sub

    I added the following statement to the constructor:

    AddHandler AppDomain.Curre ntDomain.Unhand ledException,
    AddressOf UnhandledExcept ionHandler

    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?

    Comment

    Working...