Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent visual studio from poping up when my BackgroundWorker throws an error

When debugging my application I see messages like this all the time:

An exception of type 'xxxx.xxxxx' occurred in xxxxx.exe but was not handled in user code.

The problem is that I have BackgroundWorkers that throw exceptions in their DoWork, these that are then handled by checking the RunWorkerCompletedEventArgs.Error in the RunWorkerCompleted event - and it works great at runtime.

Is there any way to prevent visual studio from showing these as "unhandled"?

Is this not the correct way to return errors from DoWork back to the UI?

I tried making my exception extend ApplicationException and unticking the box next to ApplicationException in the exceptions dialog but it still shows up.

like image 880
Adam Butler Avatar asked Oct 23 '22 20:10

Adam Butler


1 Answers

You need to catch and handle exceptions inside the methods that your DoWork method calls. My recommendation would be to catch the exception and then use the ReportProgress event to report it back to the interface for smooth handling/notification. You don't ever want to "swallow" an exception, but this reporting will allow you to gracefully log the exception or notify the user in a less intrusive manner.

Keep in mind, you'll need to to use the overload of ReportProgress that allows the use of a custom userState so that you can either report proper progress or attach the full exception.

like image 177
Joel Etherton Avatar answered Oct 31 '22 17:10

Joel Etherton