Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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 SubEnd Class