i have the following code:
Task load = Task.Factory.StartNew(() => {//Some Stuff Which Throws an Exception});
try
{
load.Wait();
}
catch (AggregateException ex)
{
MessageBox.Show("Error!");
}
When ever an exception is thrown in the Task, I want it to bubble up get caught in the try catch instead of visual studio breaking at the point the exception is caused.
I Tried google and some suggested I add this [DebuggerHidden]
on top of my method but it doesn't work
From the point of view of VS there really isn't any different between the exception being thrown from within a delegate pass to a Task from any other exception.
There is no way to solve this in the general case.
However, the one thing that you could do is leverage the fact that when the exception is re-thrown it's wrapped in an
AggregateException
. You could break when anAggregateException
is thrown but not other exceptions.You can go to Debug -> Exceptions, unselect all CLR exceptions, but then re-enable aggregate exceptions:
It will now not pause in the
Task
body but will pause the debugger when you callWait
.The unfortunate side effect is that you'll now no longer pause for any other exceptions anywhere else in your program, even if they're not in a delegate passed to a
Task
.To turn off stop on exceptions press " Ctrl + Alt + E ". This will open the Exceptions window . Untick "Common Language Runtime Exceptions - Thrown".
Ok I found out how to do it. The answer is right here in the note section