I've got a problem with finding exceptions that I can't seem to get any help via google for. Unsurprising as Visual Studio can't seem to decide whether it can find these types or not.
This is a Winforms app that I'm trying to add a reasonable (temporary!) top level try-catch to. However, despite not needing to import any namespaces AND VS being indecisive, it will not compile with these exceptions in place. If you see the first image, it's decided they don't exist.
Public Sub MainTry(action As Action)
Dim subname = If(NameOf(action) IsNot Nothing, NameOf(action), "anonymous")
Catch ex As Application.Current.DispatcherUnhandledException 'Also tried Catch ex As Application.ThreadException
ErrorHelper.Log(ex, "ThreadException", subname)
Catch ex As System.AppDomain.UnhandledException
ErrorHelper.Log(ex, "", subname)
Catch ex As Exception
ErrorHelper.Log(ex, "General Exception", subname)
If MsgBox("Do you wish to restart TMS?", MsgBoxStyle.Exclamation, app_name) = MsgBoxResult.Yes Then
Those are not exceptions. If you look at your picture it says:
Event Application.ThreadException As ThreadExceptionEventHandler
Application.ThreadException are events that are raised when an exception occurs that is not caught by a
Try/Catch statement (this is also the reason why they are events).
If you want to handle them you can subscribe to the events in the form's
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException AddHandler Application.ThreadException, AddressOf Application_ThreadException End Sub Private Sub AppDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) ErrorHelper.Log(DirectCast(e.ExceptionObject, Exception), "UnhandledException", "") End Sub Private Sub Application_ThreadException(sender As Object, e As ThreadExceptionEventArgs) ErrorHelper.Log(e.Exception, "ThreadException", "") End Sub
According to this answer and the documentation the difference between the two events is that
Application.ThreadException is fired when an exception is unhandled in a WinForms owned thread (those exceptions you can choose to ignore and execution will continue), whereas the
AppDomain.UnhandledException event is raised once an exception is unhandled in a non-WinForms thread or when the
ThreadException is disabled.
Application.ThreadException, when an unhandled exception occurs that causes the
AppDomain.UnhandledException event to be raised, the program execution cannot continue.