Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

I use:

 

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.

Posted
What gives? What would cause these 2 'Catch-All' handlers from not catching a System.NullReferenceException?

 

Could it be that you're not specifically trying (no pun) to catch a NullRefEx? So the catch is not caught? (sorry, more puns)

  • Leaders
Posted
Could it be that you're not specifically trying (no pun) to catch a NullRefEx? So the catch is not caught? (sorry' date=' more puns)[/quote']

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.

[sIGPIC]e[/sIGPIC]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...