Richard Griffiths Richard Griffiths - 4 months ago 31 Question

Schoolboy issue with exceptions in VB winforms - Can't find exception types?

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.
Now it's fine - it's lying
Now it's not happy

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
End If
End Try

End Sub

If anyone can explain why VS 2015 is being such a pain about these exception types I'd be grateful.

The project does use Office Interop and Devexpress winforms - the tooltip help does pop up with alternatives from these libraries, I've tried being explicit with naming and so on, this hasn't helped!


Those are not exceptions. If you look at your picture it says:

Event Application.ThreadException As ThreadExceptionEventHandler

AppDomain.UnhandledException and 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 Load event:

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.

Apart from Application.ThreadException, when an unhandled exception occurs that causes the AppDomain.UnhandledException event to be raised, the program execution cannot continue.