Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi,
I've a background worker which downloads data from database. If some database error occurs in Do_Work() function, I'm unable to get that error in RunWorkerCompleted() method. Now it's simply getting break showing un handled exception.

if some error occurs in do_work(),it'd break from there and an exception should be raised in RunWorkerCompleted() method.. but it's just breaking from the point it gets an error. how to do it?
Posted

Exceptions thrown in your DoWork method are caught and put in the Error property of the RunWorkerCompleteEventArgs of the RunWorkerComplete event you're handling. They won't be raised as an Exception, so any Try/Catch block you have in RunWorkerComplete won't see them.
 
Share this answer
 
Comments
DEB4u 27-Jan-13 12:25pm    
ok, If i remove the Try catch inside Do_Work(), then also it's not working, i mean program is breaking off
Dave Kreskowiak 27-Jan-13 23:13pm    
If you're running it under the Debugger, then yes, it'll stop showing you the error. Just hit the Continue button in the debugger and it'll keep running and do what it's supposed to do, put the Exception in the event args and pass it to RunWorkerCompleted.
This works just fine and as expected:
Imports System.Threading
Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        bw1.RunWorkerAsync()
    End Sub

    Private Sub bw1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw1.RunWorkerCompleted
        Debug.WriteLine("RunWorkerCompleted")
        If e.Error IsNot Nothing Then
            Debug.WriteLine("An error was thrown...")
            Debug.WriteLine(e.Error.ToString)
        Else
            Debug.WriteLine("No error was detected.")
        End If
    End Sub
    Private Sub bw1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles bw1.DoWork
        Debug.WriteLine("Sleeping for 3 seconds...")
        Thread.Sleep(3000)
        Debug.WriteLine("Throwing...")
        Throw New ArgumentNullException("someDummyVariable")
    End Sub
End Class


The result:
Sleeping for 3 seconds...
Throwing...
A first chance exception of type 'System.ArgumentNullException' occurred in BackgroundWorker Sandbox.exe
RunWorkerCompleted
An error was thrown...
System.ArgumentNullException: Value cannot be null.
Parameter name: someDummyVariable
   at BackgroundWorker_Sandbox.Form1.bw1_DoWork(Object sender, DoWorkEventArgs e) in C:\Users\Dave\Documents\Visual Studio 2012\Projects\BackgroundWorker Sandbox\BackgroundWorker Sandbox\Form1.vb:line 25
   at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
   at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900