0

In desktop application I have set up trivial

If someone wants the codez (nothing interesting there):

Namespace My

    Partial Friend Class MyApplication

        ' Thread exception handler. Handles all unhandled exceptions on UI thread.
        Private Sub UIThreadException(sender As Object, e As ThreadExceptionEventArgs)
            CustomErrorHandler.Handle(e.Exception, "[UIThreadException]")
        End Sub

        ' Handle the UI exceptions by showing a dialog box, and asking the user whether
        ' or not they wish to abort execution.
        ' NOTE: This exception cannot be kept from terminating the application - it can only 
        ' log the event, and inform the user about it. 
        Private Sub CurrentDomain_UnhandledException(sender As Object,
                                                     e As UnhandledExceptionEventArgs)
            Try
                Dim ex As Exception = CType(e.ExceptionObject, Exception)
                CustomErrorHandler.OnUnhandled(ex)
            Catch exc As Exception
                MsgBox(exc.ToString(), vbCritical, "Last resort error printout")
            End Try
        End Sub

    End Class

End Namespace

Sometimes, there is a difference when I throw exception from Form1's ButtonOK_Click(...) event handler:

  • when launched as standalone (app.exe), then UIThreadException() is called as expected
  • when launched from inside Visual Studio (app.vshost.exe), then CurrentDomain_UnhandledException() is called in the same scenario (the same place in code as before, the same data), ignoring UIThreadException() handler

So in standalone exe, all works reliably, but during debugging session, falling into unhandled exception in the same cases is very annoying.

I could observe the same behavior in Visual Studio 2012 and 2015. If tested on trivial examples, it always works as expected also in debugging session. If code is more complex (exception thrown in nested call etc.) then UI thread exception handler gets ignored.

Was someone of you able to observe the same problem and found a solution?

miroxlav
  • 11,796
  • 5
  • 58
  • 99

1 Answers1

0

The exception was thrown in OnLoad() event (and I did not notice it).

Some inexperienced coworker put SQL processing into OnLoad event handler instead of constructor or OnShow event handler. And this is where troubles begin. Yes, I've once been there.

Community
  • 1
  • 1
miroxlav
  • 11,796
  • 5
  • 58
  • 99