Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 27-Jan-13 2:21am
DEB4u583
Rate this: bad
good
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.
  Permalink  
Comments
DEB4u at 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 at 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.
Rate this: bad
good
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
        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)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 425
1 DamithSL 218
2 Dave Kreskowiak 200
3 Maciej Los 195
4 arvind mepani 195


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid