title: Unhandled exceptions in BackgroundWorker
body: <p>I have a small WinForms app that utilizes a BackgroundWorker object to perform a long-running operation.</p>
  
  <p>The background operation throws occasional exceptions, typically when somebody has a file open that is being recreated.</p>
  
  <p>Regardless of whether the code is run from the IDE or not .NET pops up an error dialog informing the user that an Unhandled exception has occurred.  Compiling the code using the Release configuration doesn't change this either.</p>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.dowork.aspx" rel="noreferrer" title="MSDN">MSDN</a>:</p>
  
  <blockquote>
    <p>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. If 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.</p>
  </blockquote>
  
  <p>I expect these exceptions to be thrown on occasion and would like to handle them in the RunWorkerCompleted event rather than in DoWork.  My code works properly and the error is handled correctly within the RunWorkerCompleted event but I can't for the life of me figure out how to stop the .NET error dialog complaining about the "Unhandled exception" from occurring.</p>
  
  <p>Isn't the BackgroundWorker supposed to catch that error automagically?  Isn't that what the MSDN documentation states?  What do I need to do to inform .NET that this error <em>is</em> being handled while still allowing the exception to propage into the Error property of RunWorkerCompletedEventArgs?</p>