Create a new windows forms project (in using vb.net 2010).
Create a button and a background worker on the form.
Under the Debug > Exceptions menu, make sure that for 'Common Language Runtime Exceptions' you have 'Thrown' unticked and 'User-unhandled' ticked.
Now put the following code in the form's code-behind;
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Dim X As Integer
X = 5 / X
Why does it not error nicely? It comes up with no helpful stack trace and just says 'Exception has been thrown by the target of an invocation.'.
Place the code to cause the error into the Button_Click event and run it and you will see the difference. Why do we not get these 'nice' errors given that the runworkercompleted event is supposed to be running on the main thread?
View Complete Post