I know this is a bit similar to the question Background worker exception handling but it is a bit different.
So based on my understanding of backgroundworkers when an exception occurs in the dowork() method the exception is passed to e.Error
in the RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e).
My question has a few parts.
1.) Will do work cease executing all other code after the line where the exception happened and then pass to RunWorkerCompleted
? And is it necessary/best practice to use try/catch in DoWork()
to ensure this behavior?
2.) When throwing from inside the the DoWork()
or RunWorkerCompleted()
methods where are the exceptions being thrown too.
Example:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
//Should I be putting code in this as well as getting the exception in `RunWorkerCompleted()`?
//Or is the already how the background worker works already with out me needing to explicitly put the try/catch?
}
catch (Exception ex)
{
throw ex; //is this throwing to the 'RunWorkerCompleted()` or outside the thread to error handling on in the thread where RunWorkerAsync() was called?
}
}
private void backgroundowrker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null
{
throw e.Error; //This throws it back to the thread that called `RunWorkerAsync()` right?
}
}