InnerException > BackgroundWorker > MyApplication.UnhandledExcepti



I'm trying to understand why the MyApplication.UnhandledException event
handler behaves differently when the exception originates from the background
thread of a BackgroundWorker component.

e.g.

The event handler displays the message from the exception and from the inner
exception:

Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal
e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
Handles Me.UnhandledException
If e.Exception.InnerException Is Nothing Then
MsgBox(e.Exception.Message & " Nothing")
Else
MsgBox(e.Exception.Message & " " & e.Exception.InnerException.Message)
End If
e.ExitApplication = False
End Sub
End Class

The following code behaves as expected, the event handler displaying “Blah
Foo”.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Try
Throw New Exception("Foo")
Catch ex As Exception
Throw New Exception("Blah", ex)
End Try
End Sub

But the following code behaves unexpectedly, the event handler displaying
“Foo Nothing”, which, I guess, comes as a result of the base exception being
passed to the event handler.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Me.BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As
System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Try
Throw New Exception("Foo")
Catch ex As Exception
Throw New Exception("Blah", ex)
End Try
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object,
ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles
BackgroundWorker1.RunWorkerCompleted
Throw e.Error
End Sub

I’d appreciate an explanation as to why and also an indication as to how I
can get the complete exception tree.

.