UnhandledException / ThreadException not working

kasdoffe

Regular
Joined
Aug 27, 2003
Messages
57
I have an UnhandledExceptionHandler and ThreadException handler wrapped around my main form in the Main method.

However, I can reproduce an error that doesn't get caught by these 2 handlers. What gives? What would cause these 2 'Catch-All' handlers from not catching a System.NullReferenceException?

PS. I've read that these handlers are 'Catch-All' handlers. I'm not so sure now.
 
I use:

Visual Basic:
Sub Main()
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf OnUnhandledException
    AddHandler Application.ThreadException, AddressOf OnGuiUnhandedException

    Application.Run(New yourForm)
End Sub

Sub OnUnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
		HandleException(e.ExceptionObject)
	End Sub


	Sub OnGuiUnhandedException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
		HandleException(e.Exception)
	End Sub

	Public Sub HandleException(ByRef ex As Exception)
		Dim frmException As New frmException(ex)
		frmException.ShowDialog()
		frmException.Close()
		frmException.Dispose()
		Application.Exit()
	End Sub

This seems to catch all exceptions!

B.
 
handlewithbeer said:
Could it be that you're not specifically trying (no pun) to catch a NullRefEx? So the catch is not caught? (sorry, more puns)
If you add an unhandled exception handler and a gui unhandled exception handler then you would be trying to catch a NullReferenceException or any exception regardless of type. I am as confused as you, but I have never used an unhandled exception handler, so I'm not speaking from experience. Try throwing a NullReferenceException from within Main() and see if it is caught by your unhandled exception delegate. Also try adding a try/catch block in main to catch this elusive NullReferenceException that is giving you trouble. In my experience, when things like this don't work as expected, trying different things and comparing the circumstances between what works and doesn't can shed light on the reason why your program isn't working as expected.
 
Back
Top