Catching unhandled exceptions in .Net 2.0. Invalid event
I have a little bit of an understanding of how unhandled .Net exceptions are handled and I am getting unexpected results that I would like to share with you to see what you think of them.
The first one is pretty easy to see. I wrote this code to execute a test, just a button that throws an exception on the same thread that created the form:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Throw New Exception()
End Sub
Private Sub UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
MsgBox(String.Format("Exception: {0}. Ending: {1}. AppDomain: {2}", CType(e.ExceptionObject, Exception).Message, e.IsTerminating.ToString(), AppDomain.CurrentDomain.FriendlyName))
End Sub
Private Sub UnhandledThreadException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
MsgBox(String.Format("Exception: {0}. AppDomain: {1}", e.Exception.Message(), AppDomain.CurrentDomain.FriendlyName))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledException
AddHandler Application.ThreadException, AddressOf UnhandledThreadException
End Sub
End Class
When I run the code inside Visual Studio, an UnhandledException is thrown as expected, but when I run the application from Windows, an UndhanledThreadException is thrown instead. ¿? ¿? ¿¿? ¿?
Does anyone have any idea what might be going on here?
Thanks in advance.
EDIT: After reading the documentation, Application.ThreadException looks like Application.ThreadException is thrown when exceptions happen inside "Windows Forms threads" (whatever they are, IMHO there is only one Windows Form thread in each application). Thus, Application.ThreadException refers to Exceptions thrown from the thread that created your application's form, and other exceptions are handled by the AppDomain.CurrentDomain.UnhandledException.
source to share
Yes, it is normal. Capturing Application.ThreadException is disabled when you run it under the debugger. This is done so that you can easily diagnose exceptions. To make it behave the same, you must call the Application.SetUnhandledExceptionMode () method . Unfortunately this is difficult to do in a VB.NET project, you need to disable the application infrastructure.
Not worth the hassle, press Ctrl + F5 if you want to test your exception handling code.
source to share