Unhandled Exception Handler Changes in .NET 2.0

Provided by Dave Juth, Senior Systems Architect

WinForms applications written in .NET 1.x and converted to .NET 2.0 that have a global unhandled exception handler need to be modified to trap the exception.

An example of the 1.x way to implement a global exception handler follows:

(in Sub Main):

    AddHandler Application.ThreadException, AddressOf OnThreadException

The exception handler:

 

    Public Sub OnThreadException(ByVal sender As Object, _

        ByVal t As System.Threading.ThreadExceptionEventArgs)

 

        LogError(t.Exception)

        Application.Exit()

 

    End Sub

 

In .NET 2.0, this code will not trap the exception. The .NET 2.0 framework requires hooking AppDomain.CurrentDomain.UnhandledException instead:

(in Sub Main):

    AddHandler AppDomain.CurrentDomain.UnhandledException, _

        AddressOf OnUnhandledException

The exception handler itself can be implemented as shown below:

      Public Sub OnUnhandledException(ByVal sender As Object, _

          ByVal t As UnhandledExceptionEventArgs)

 

          Logger.LogError(t.ExceptionObject)

          Application.Exit()

 

      End Sub


Additional Resources

 

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.


View all FMS products for Microsoft Access All Our Microsoft Access Products

 

 

Free Product Catalog from FMS