There is MS documentation about trap exception https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.application.threadexception?view=windowsdesktop-7.0
But this documentation is theoretical and don't bound to Windows Forms and Visual Studio 2022, In Visual studio we have special button to handle exception
This class allow to trap only one exception UnhandledException. I need to trap second exception, because I have sophisticated multiThreading application.
I created this simple test to trap ThreadException
Public Shared Sub ThreadExceptionEventHandler(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
MsgBox(e.Exception, MsgBoxStyle.OkOnly, "ThreadExceptionEventHandler")
End Sub
Private Async Sub MainForm_Load(sender As Object, e As EventArgs) Handles Me.Load
AddHandler Application.ThreadException, AddressOf ThreadExceptionEventHandler '
Await Task.Run(Sub()
Throw New Exception("test exception")
End Sub)
....
and in class MyApplication try to trap Unhadled exception
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
MsgBox(e.Exception, MsgBoxStyle.OkOnly, "UnhandledException")
End Sub
...
So, it's look as any exception must catch by my code, isn't it? But no. Starting code with Visual studio show unhandled error
Starting program on console don't show anything, program shutdown without any error message, this means trap any exception don't working. Why?
Also. pay attention that ThreadException not available in class MyApplication.



According @Jimi advice, final solution is there
Add 2 small comments to Jimi advice:
(1) Msgbox (and maybe all windows forms) don't working in this workflow, I have simple write exception on file, in this case it's working.
(2) alternative way I use very often in multiThreading application (but unfortunately alternative way is impossible in my current program) - is starting Windows forms app as console application, and than console application open windows forms further, after binding to all windows forms event will done. In this case black console screen appears on short time and only than Windows forms appears.
but with overloading or overriding onStartup on MyApplication is workable and more simple way, thanks Jimi. And, of course, if we want to trap ONLY thread exception, trap common Unhandled exception need to comment.
according @Jimi comment, overriding is also workable solution