I am trying to wrap the exceptions that can be thrown by an async task using ContinueWith(). If I just throw from the continuation action things seem to work, but my debugger claims the exception is unhandled. Am I doing something wrong or is this a Visual Studio problem? Is there a cleaner way to do this, or a way to work around my debugger stopping on what is ultimately a handled exception?
The test below passes and prints “caught wrapped exception as expected”, but when I debug it the throw new CustomException line shows as “unhandled by user code”.
var task = DoWorkAsync().ContinueWith(t => {
throw new CustomException("Wrapped", t.Exception.InnerException); // Debugger reports this unhandled
}, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);
try {
task.Wait();
Assert.Fail("Expected work to fail");
} catch (AggregateException ag) {
if (!(ag.InnerException is CustomException))
throw;
}
Console.WriteLine("Caught wrapped exception as expected");
When “Just My Code” is enabled, Visual Studio in some cases will break on the line that throws the exception and display an error message that says “exception not handled by user code.” This error is benign. You can press F5 to continue and see the exception-handling behavior that is demonstrated in these examples. To prevent Visual Studio from breaking on the first error, just uncheck the “Just My Code” checkbox under Tools, Options, Debugging, General.
From http://msdn.microsoft.com/en-us/library/dd997415.aspx