Results 1 to 9 of 9

Thread: Handling unhandled exceptions part 1

Threaded View

  1. #1

    Thread Starter
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Handling unhandled exceptions part 1

    One of the nice features hidden within My namespace under MyApplication is an event called UnhandledException which allows a developer to handle unhandled exceptions by writing code to display a user friendly message, write to a log file and perhaps send an email to the developer informing them of the problem.

    Rather dig straight into a full blown replacement for the default unhandled exception handler I will present them incrementally beginning with showing the exception in a simple web browser embedded in a Windows Form with the stack trace displayed. The final installment will have a highly customized user friendly message box, write each unhandled exception to an XML file and provided email capabilities where logging and email features will be controlled via an unhandled exception handler configuration file which is read from a DLL containing the unhandled exception handler.

    To implement a replacement unhandled exception handler go to My Project windows of your project and select the Application tab, select the button “View Application Events” which creates a module with an empty partial class for MyApplication. Select the upper left hand corner ComboBox for Classes and select (MyApplicationEvents) followed by selecting the Method ComboBox in the upper right hand corner of the code window and select unhandledexceptions which creates an event procedure for overriding how unhandled exceptions are handled.

    IMPORTANT The following does not work if you are trying this code out from the IDE but instead must run without the IDE debugger. In the attached demonstration project there is code to prevent you from attempting testing from the IDE.

    Code to prevent running in the IDE.
    Code:
    If My.Application.RunningUnderDebugger Then
       My.Dialogs.InformationDialog("Can not demo within the IDE")
    Else
       EndupHere()
    End If
    
    Namespace My
       Partial Friend Class MyApplication
          ''' <summary>
          ''' Indicates if we are running under the IDE or not
          ''' </summary>
          ''' <value></value>
          ''' <returns></returns>
          ''' <remarks></remarks>
          Public ReadOnly Property RunningUnderDebugger() As Boolean
             Get
                Return System.Diagnostics.Debugger.IsAttached
             End Get
          End Property
    . . .
    Using the following code will pretty much do nothing other than demonstrate we have control over the exception.

    Code:
    Private Sub MyApplication_UnhandledException( _
       ByVal sender As Object, _
       ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
       MsgBox(e.Exception.ToString)
    End Sub
    From here we could write the exception to a log file as shown below which is not bad but we can improve on the formatting and make the file easier to read along with setting it up to be viewable in a exception log file viewer using a Data bound DataGridView where the stack trace is displayed in a control beneath the DataGridView. For now we will use a simple format.

    Code:
    Private Sub MyApplication_UnhandledException( _
       ByVal sender As Object, _
       ByVal e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
    
       Dim Doc As New XDocument
       Dim FileName As String = "unhandledExceptions.xml"
    
       If IO.File.Exists(FileName) Then
          Doc = XDocument.Load(FileName)
       Else
          Doc = XDocument.Parse( _
          "<?xml version=""1.0"" encoding=""utf-8""?><Exceptions></Exceptions>")
       End If
       Dim config = Doc.<Exceptions>(0)
    
       config.Add(<Exception>
                     <Date_Time><%= Now %></Date_Time>
                     <Message><%= e.Exception.Message %></Message>
                     <StackTrace><%= Environment.NewLine %>
                        <%= e.Exception.StackTrace.ToString %>
                     </StackTrace>
                  </Exception>)
    
       config.Save(FileName)
    
       MsgBox( _
          String.Format( _
          "An exception happened which will close this program{0}{1}", _
          Environment.NewLine, e.Exception.ToString))
    End Sub
    Attached Files Attached Files
    Last edited by kareninstructor; Jan 12th, 2011 at 07:23 PM. Reason: Added project file

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width