11

I am slightly confused on how to deal with an exception.

I have a background worker thread that runs some long running process. My understanding is if an exception occurs on the background worker thread the code will still end up at the RunWorkerCompleted method.

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        if (e.Error != null)
           throw e.Error;

If this is the case is there any point in putting a try catch block around the bgWorker.RunWorkerAsync(); call, I assume not?

I want to rethrow the exception that is caught in the RunWorkerCompleted method, how can I do this without losing the stack trace - is what I have above correct? I read that you when rethrowing an exception you should just use "throw"?

Ric
  • 12,855
  • 3
  • 30
  • 36
mHelpMe
  • 6,336
  • 24
  • 75
  • 150

3 Answers3

16

I suggest you to create some business specific exception, which describes operation which you were doing in background. And throw this exception with original exception as inner exception:

private void bgWorker_RunWorkerCompleted(
    object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
        throw new BusinessSpecificException("Operation failed", e.Error);
    // ...
}

Thus original exception with its stack trace will be available, and you'll have more descriptive exception thrown.

Note - if you don't want to create new exception class, you can use existing ApplicationException or Exception. But its not that informative and if you are going to catch it somewhere, then you'll not be able to catch this particular exception only

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
8

Try this

void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {

        if (e.Error != null)
           throw new Exception("My Custom Error Message", e.Error);
Jade
  • 2,972
  • 1
  • 11
  • 9
2

If this is the case is there any point in putting a try catch block around the bgWorker.RunWorkerAsync(); call, I assume not?

No you can't do this because bgWorker.RunWorkerAsync(); it's a method (not an event). f you are running under the Visual Studio debugger, the debugger will break at the point in the DoWork event handler where the unhandled exception was raised. So you can do something like this

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                //put your break point here 
                 // here you can capture your exception  
            }
            catch (Exception ex)
            {
                // here  catch your exception and decide what to do                  
                throw;
            }



        }
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • so in this case I wouldn't need to do any error handling in my RunWorkerCompleted method? Or is it better practise to deal with any errors in the RunWorkerCompleted method? – mHelpMe Dec 14 '13 at 11:20
  • 1
    if you handle the exception in your worker thread you won't be able to get it in RunWorkerCompleted and if you don't handle it in DoWork, your application may crash if you do something wrong – BRAHIM Kamel Dec 14 '13 at 12:23