The Moth - AppDomain.UnhandledException Part 2 (original) (raw)

« AppDomain.UnhandledException Part 1 | About Watchdog »

Mon, December 20, 2004, 04:01 PM under MobileAndEmbedded

Below find the VB code for global exception handling in NETCF, as described here. First though, a sort paragraph that you may skip if you desire.

It is worth noting some behaviours of surrounding Application.Run with try..catch (which we already established is not needed). On its own, it cannot catch exceptions thrown from a thread, but it can catch ones thrown on the GUI. It can also catch them when thrown in a Control.Invoke, but only after the built-in CF error dialog appears. If you use try..catch in combination with AD.UnhandledException, it gets more interesting. The try..catch handler is always hit straight after the AppDomain handler with a ThreadAbortException (and a different StackTrace); in those cases, your handling code better run fast, as the process is on its way out and even a MessageBox will not stay up for long. The exception (no pun intended) to the last comment and the order the two handlers are run, is when the exception is thrown on the GUI thread. So, once again, AppDomain.UnhandledException is all you need.

Without further ado here is the VB version:

Public NotInheritable Class EntryPoint
Private Sub New()
End Sub

 <MTAThread()> _  
Shared Sub Main()  
    ' Add Global Exception Handler  
    AddHandler AppDomain.CurrentDomain.UnhandledException, _  
                                    AddressOf OnUnhandledException

     Application.Run(New Form1)  
End Sub

 ' In CF case only, ALL unhandled exceptions come here  
Private Shared Sub OnUnhandledException(ByVal sender As Object, _  
                            ByVal e As UnhandledExceptionEventArgs)  
    Dim ex As Exception = TryCast(e.ExceptionObject, Exception)  
    If (ex IsNot Nothing) Then  
        ' Can't imagine e.IsTerminating ever being false  
        ' or e.ExceptionObject not being an Exception   
        EntryPoint.HandleException(ex, e.IsTerminating)  
    End If  
End Sub

 '' This should not throw any exceptions   
'' and should run as quick as possible  
Private Shared Sub HandleException(ByVal ex As Exception, _  
           ByVal t As Boolean)  
    ' Log ex.StackTrace or whatever  
    'MessageBox.Show(ex.StackTrace, ex.Message);  
    Dim fs As FileStream = Nothing  
    Dim sw As StreamWriter = Nothing  
    Try  
        fs = New FileStream("GEH.txt", FileMode.Append, _  
          FileAccess.Write, FileShare.None, 1000, False)  
        sw = New StreamWriter(fs)  
        sw.WriteLine("DateStamp: " + DateTime.Now.ToString())  
        sw.WriteLine("ToString(): " + ex.ToString())  
        sw.WriteLine("Message: " + ex.Message)  
        sw.WriteLine("StackTrace: " + ex.StackTrace)  
        System.Threading.Thread.Sleep(1000)             'for a laugh  
        sw.WriteLine("THE END")  
        sw.Flush()  
    Finally  
        If sw IsNot Nothing Then  
            fs.Close()  
            sw.Close()  
        End If  
    End Try  
End Sub  

End Class