kasdoffe Posted September 13, 2005 Posted September 13, 2005 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. Quote
penfold69 Posted September 14, 2005 Posted September 14, 2005 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. Quote
handlewithbeer Posted September 14, 2005 Posted September 14, 2005 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) Quote
Leaders snarfblam Posted September 15, 2005 Leaders Posted September 15, 2005 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. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.