I want to handle exceptions that happen inside of DoWork
handler in my RunWorkerCompleted
handler but while code is running under debugger I'm getting another exception first "Exeption was unhandled by user code".
Here short example of code I'm using:
BackgroundWorker _worker;
public void Test()
{
_worker = new BackgroundWorker();
_worker.WorkerReportsProgress = false;
_worker.WorkerSupportsCancellation = false;
_worker.DoWork += new DoWorkEventHandler(bw_DoWork);
_worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
_worker.RunWorkerAsync();
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = ActuallWorkHere();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var t = e.Error;
}
private IEnumerable<string> ActuallWorkHere()
{
throw new Exception("test");
// "Exeption was unhandled by user code" if called
string[] res = { "test" };
return res;
}
What am I doing wrong?