Click here to Skip to main content
13,502,277 members
Rate this:
Please Sign up or sign in to vote.
See more:
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 27-Jan-13 2:21am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
    End Sub

    Private Sub bw1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bw1.RunWorkerCompleted
        If e.Error IsNot Nothing Then
            Debug.WriteLine("An error was thrown...")
            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...")
        Throw New ArgumentNullException("someDummyVariable")
    End Sub
End Class

The result:
Sleeping for 3 seconds...
A first chance exception of type 'System.ArgumentNullException' occurred in BackgroundWorker Sandbox.exe
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)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
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 content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.180417.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100