BackgroundWorker & Exceptions

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
Hey all, another day, another stupid .NET problem.. :D

Ive got a BackgroundWorker which imports data from an Access file and does a chunk of validation on it. I throw one of a variety of custom exceptions if the data is invalid. When I run in debug mode, I always get a break in execution and an "unhandled exception" error, but running in release mode it's silently handled (which is correct!). In debug mode, you can press continue and the code will run OK.

Is there anyway to suppress this annoying break in execution? Am I doing something wrong? Grrrrr.

Example:

VB.NET:
[FONT=Courier New][COLOR=Blue]Private WithEvents[/COLOR][/FONT][FONT=Courier New] bw [/FONT][FONT=Courier New][COLOR=Blue]As New[/COLOR][/FONT][FONT=Courier New] BackgroundWorker()
    
[/FONT][FONT=Courier New][COLOR=Blue]Private Sub[/COLOR][/FONT][FONT=Courier New] frmMain_Load([/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]sender [/FONT][FONT=Courier New][COLOR=Blue]As Object[/COLOR][/FONT][FONT=Courier New], [/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]e [/FONT][FONT=Courier New][COLOR=Blue]As EventArgs[/COLOR][/FONT][FONT=Courier New]) [/FONT][FONT=Courier New][COLOR=Blue]Handles MyBase[/COLOR][/FONT][FONT=Courier New].Load
    bw.RunWorkerAsync()
[/FONT][FONT=Courier New][COLOR=Blue]End Sub[/COLOR][/FONT][FONT=Courier New]

[/FONT][FONT=Courier New][COLOR=Blue]Private Sub[/COLOR][/FONT][FONT=Courier New] run([/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]sender [/FONT][FONT=Courier New][COLOR=Blue]As Object[/COLOR][/FONT][FONT=Courier New], [/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]e [/FONT][FONT=Courier New][COLOR=Blue]As[/COLOR][/FONT][FONT=Courier New] DoWorkEventArgs) [/FONT][FONT=Courier New][COLOR=Blue]Handles [/COLOR][/FONT][FONT=Courier New]bw.DoWork
    [/FONT][FONT=Courier New][COLOR=Blue]Throw New[/COLOR][/FONT][FONT=Courier New] Exception()
[/FONT][FONT=Courier New][COLOR=Blue]End Sub[/COLOR][/FONT][FONT=Courier New]
[/FONT][FONT=Courier New][COLOR=Blue]
[/COLOR][/FONT][FONT=Courier New][COLOR=Blue]Private Sub[/COLOR][/FONT][FONT=Courier New] ending([/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]sender [/FONT][FONT=Courier New][COLOR=Blue]As Object[/COLOR][/FONT][FONT=Courier New], [/FONT][FONT=Courier New][COLOR=Blue]ByVal [/COLOR][/FONT][FONT=Courier New]e [/FONT][FONT=Courier New][COLOR=Blue]As [/COLOR][/FONT][FONT=Courier New]RunWorkerCompletedEventArgs) [/FONT][FONT=Courier New][COLOR=Blue]Handles [/COLOR][/FONT][FONT=Courier New]bw.RunWorkerCompleted
    [/FONT][FONT=Courier New][COLOR=Blue]If [/COLOR][/FONT][FONT=Courier New]e.Error [/FONT][FONT=Courier New][COLOR=Blue]IsNot Nothing Then[/COLOR][/FONT][FONT=Courier New]
        MsgBox("Exception!")
[/FONT][FONT=Courier New][COLOR=Blue]    End If
End Sub[/COLOR][/FONT]

Cheers guys
 
Go main menu 'Debug' > 'Exceptions' for a dialog. It says 'break when an exception is:' and the one in your sample is found in CLR Exceptions > System > System.Exception
 
Back
Top