January 11th, 2010

FAQ :: The Debugger does not correctly handle Task exceptions?

The following code correctly observes and handles a Task exception and should print “gotcha!” to the console.  By default though, the Debugger will report a crash.

Task t = Task.Factory.StartNew(() => { throw new Exception(“poo”); });

try { t.Wait(); }

catch (AggregateException) { Console.WriteLine(“gotcha!”); }

The issue has to do with the “Just My Code” mode (enabled by default), which causes the Debugger to break in immediately when an exception leaves user code (the Task delegate) and enters non-user code (TPL internal).  This is usually a good thing, because it allows you to pinpoint exactly where an exception is going unhandled.  However, in this case, the Debugger is breaking before TPL can observe the exception.

Running without debugging or disabling “Just My Code” (Tools -> Options -> Debugging -> General) should resolve the issue.  Also, note that the Debugger actually broke in as though it had hit a breakpoint, so Continuing (F5) or Stepping (F10/F11) should allow further execution.

Just My Code Mode

just-my-code.png

Author

0 comments

Discussion are closed.