Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackgroundWorker & Exceptions

The MSDN Docs says

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into the RunWorkerCompleted event handler, where it is exposed as the Error property of System.ComponentModel.RunWorkerCompletedEventArgs

But when I tried

_workers[i].DoWork += (s, args) =>
{
    throw new Exception("Error!");
};

I get Exception Unhandled error ... The code doesn't seem to go to RunWorkerCompleted. How are errors supposed to be handled?

UPDATE

I have setup simple handlers

_workers[i].DoWork += (s, args) =>
{
    throw new Exception("Error!");
}
...
_workers[i].RunWorkerCompleted += (s, args) =>
{
    if (args.Error != null) {
        string dummy = args.Error.Message;
    }
    ...
};

The code never leaves DoWork

like image 411
Jiew Meng Avatar asked Nov 25 '10 09:11

Jiew Meng


1 Answers

If an exception is thrown and passes through user frames but is then not caught by user code, it is considered "user-unhandled".

So I think there are 3 possibilities:

  • This is a first-chance exception - so if you press F5, the exception will propagate as normal.
  • Click "edit code" in the Exception Assistant. Then do some Edit and Continue to solve the problem.
  • Go to Debug->Exceptions and de-select the "user-unhandled" column.
like image 136
HTTP 410 Avatar answered Sep 28 '22 07:09

HTTP 410