Geeks With Blogs

News
Neat Stuff Read all my hurricane entries While you are here, visit the Geeks With Blogs main feed
Advertising
Links Status of the Navy
Channel 9

.NET Hobbyist Programmer Staying Confused in a Busy World

OK, how does someone stupid proof an unhandled exception handler?  As you can see below, I am trapping two events with these handlers.

    Public Sub OnThreadException( _
      ByVal sender As Object, _
     
ByVal e As System.Threading.ThreadExceptionEventArgs)
    Public Sub OnUnhandledException( _
      ByVal sender As Object, _
      ByVal e As System.UnhandledExceptionEventArgs)

I think these are correct for a WinForms app.  I check the event args for Nothing and e.Empty before processing since this seems prudent.  Within the CommonHandler routine, I assume an exception there should lead to application termination.  Otherwise, I proceed with setting up and displaying and ExceptionForm (not included here) that acts as the exception display in place of the standard .NET dialog.

I am a little uneasy about this part:

    CType(e.ExceptionObject, System.Exception)

I have not been able to get the IDE to let me test it yet.  I am still hopeful though.  Again, logging is stubbed out for later modification.  Most of this class is pretty easy and seems too cover what is needed, but are there any lessons learned out there that can improve or modify it?


===== Start File UnhandledException.vb =====

Option Explicit On
Option Strict On

Imports System.Windows.Forms

Namespace Exception

  Public Class GlobalExceptionHandler

#Region " Class Data "

    Private m_UnhandledExceptionHandlerAdded As Boolean = False
    Private m_Text As String
    Private currentDomain As AppDomain = AppDomain.CurrentDomain

#End Region

#Region " Event Handlers "

    Public Sub OnThreadException( _
        ByVal sender As Object, _
        ByVal e As System.Threading.ThreadExceptionEventArgs)
      m_Text = "Thread Exception"
      If ((e Is Nothing) Or (e Is e.Empty)) Then ErrorExit(m_Text)
      CommonHandler(sender, e.Exception, m_Text)
    End Sub

    Public Sub OnUnhandledException( _
        ByVal sender As Object, _
        ByVal e As System.UnhandledExceptionEventArgs)
      m_Text = "Unhandled Exception"
      If ((e Is Nothing) Or (e Is e.Empty)) Then ErrorExit(m_Text)
      CommonHandler(sender, CType(e.ExceptionObject, System.Exception), m_Text)
    End Sub

#End Region

#Region " Private Routines "

    Private Sub CommonHandler( _
        ByVal sender As Object, _
        ByVal e As System.Exception, _
       
ByVal text As String)
      Dim result As DialogResult = System.Windows.Forms.DialogResult.Cancel
      If (System.Configuration.ConfigurationSettings.AppSettings.Get _
          ("DisplayExceptions") = "Developer") Then
        ' Show Developer information
        Try
          Dim frm As New ExceptionForm(e, True)
          result = frm.Show(True)
        Catch exLocal As System.Exception
          ' Since we have an exception in our exception handler, we Exit
          Try
            DoLogging(String.Empty)
            MessageBox.Show( _
              "Fatal Error - Unhandled error in GlobalExceptionHandler" & _
              vbCrLf & vbCrLf & exLocal.GetBaseException.ToString, _
              "Fatal Error", _
              MessageBoxButtons.OK, _
              MessageBoxIcon.Stop)
          Finally
            Application.Exit()
          End Try
        End Try
      Else ' If not Developer
        ' Show User information
        DoLogging(String.Empty)
        'TODO: Handle user display case
        MessageBox.Show("User Friendly Message")
      End If
      ' Exits the program when the user clicks Abort
      If (result = System.Windows.Forms.DialogResult.Abort) Then Application.Exit()
    End Sub

    Private Sub ErrorExit(ByVal message As String)
      Try
        DoLogging(String.Empty)
        MessageBox.Show( _
          "Fatal Error - Empty EventArgs in " + message + " - Exiting the program", _
          "Fatal Error", _
          MessageBoxButtons.OK, _
          MessageBoxIcon.Stop)
      Finally
        Application.Exit()
      End Try
    End Sub

#End Region

#Region " Handler Control "

    Public Sub AddNewHandler()
      If (Not m_UnhandledExceptionHandlerAdded) Then
        AddHandler _
          System.Windows.Forms.Application.ThreadException, _
          New System.Threading.ThreadExceptionEventHandler(AddressOf OnThreadException)
        AddHandler _
          AppDomain.CurrentDomain.UnhandledException, _
          AddressOf OnUnhandledException
        m_UnhandledExceptionHandlerAdded = True
        DoLogging("AddHandler GlobalExceptionHandler")
      End If
    End Sub

    Public Sub RemoveCurrentHandler()
      If (m_UnhandledExceptionHandlerAdded) Then
        RemoveHandler _
          System.Windows.Forms.Application.ThreadException, _
          New System.Threading.ThreadExceptionEventHandler(AddressOf OnThreadException)
        RemoveHandler _
          currentDomain.UnhandledException, _
          AddressOf OnUnhandledException
        m_UnhandledExceptionHandlerAdded = False
        DoLogging("RemoveHandler GlobalExceptionHandler")
      End If
    End Sub

#End Region

    Private Sub DoLogging(ByVal message As String)
      ' Future expansion
    End Sub

  End Class

End Namespace

===== End File UnhandledException.vb =====

Posted on Sunday, March 28, 2004 6:26 PM Programming | Back to top


Comments on this post: Unhandled Exception Handler

# re: Unhandled Exception Handler
Requesting Gravatar...
any news on this? I am looking at the same thing. want to use the MS exception block and a global trap.
Left by denny on Dec 12, 2004 9:29 AM

# re: Unhandled Exception Handler
Requesting Gravatar...
To get an UnHandledException, create a new thread: while this thread is running, abort it.
Left by jadepedro on Jan 26, 2005 6:50 AM

# re: Unhandled Exception Handler
Requesting Gravatar...
what's inside ExceptionForm??
Left by pazz on Aug 27, 2010 3:12 AM

# re: Unhandled Exception Handler
Requesting Gravatar...
the 'CType(e.ExceptionObject, System.Exception)' is correct..
Left by pazz on Sep 08, 2010 2:00 AM

# re: Unhandled Exception Handler
Requesting Gravatar...
Please reply.. This Exception Handler is working!!!! it detects error.. but when I close the message "User Friendly Message". the VB.net still stop debugging..

sorry for rough english..
Left by pazz on Sep 08, 2010 2:22 AM

# re: Unhandled Exception Handler
Requesting Gravatar...
No one here.. I found a SOLUTION

1. Uncheck the ENABLE APPLICATION FRAMEWORK in 'My Project >> Application' Setting
2. Set your Startup form into 'Sub Main'
4. Add the UnhandledException.vb <ABOVE> code.
3. Create a module 'MyGlobal' or just add this code into your existing module

===============Module MyGlobal.vb=======================

Module MyGlobal
Public GlobExcp As New Exception.GlobalExceptionHandler

Public Sub Main()

Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, True)
GlobExcp.AddNewHandler()
Application.Run(New Form1)
End Sub
End Module


===============End Module MyGlobal.vb=====================

END END END END END END END END END END
Left by pazz on Sep 08, 2010 3:21 AM

Your comment:
 (will show your gravatar)


Copyright © Mark Treadwell | Powered by: GeeksWithBlogs.net