{"id":3091,"date":"2010-01-11T12:09:00","date_gmt":"2010-01-11T12:09:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/pfxteam\/2010\/01\/11\/faq-the-debugger-does-not-correctly-handle-task-exceptions\/"},"modified":"2010-01-11T12:09:00","modified_gmt":"2010-01-11T12:09:00","slug":"faq-the-debugger-does-not-correctly-handle-task-exceptions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/faq-the-debugger-does-not-correctly-handle-task-exceptions\/","title":{"rendered":"FAQ :: The Debugger does not correctly handle Task exceptions?"},"content":{"rendered":"<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt\"><span style=\"color: black\"><span style=\"font-family: Calibri;font-size: small\">The following code correctly observes and handles a Task exception and should print &ldquo;gotcha!&rdquo; to the console.<span>&nbsp; <\/span>By default though, the Debugger will report a crash.<br \/><\/span><\/span><span style=\"color: #2b91af\"><br \/><\/span><span style=\"line-height: 115%;font-family: Consolas;color: #2b91af;font-size: 10pt\">Task<\/span><span style=\"line-height: 115%;font-family: Consolas;font-size: 10pt\"> t = <span style=\"color: #2b91af\">Task<\/span>.Factory.StartNew(() =&gt; { <span style=\"color: blue\">throw<\/span> <span style=\"color: blue\">new<\/span> <span style=\"color: #2b91af\">Exception<\/span>(<span style=\"color: #a31515\">&#8220;poo&#8221;<\/span>); });<span style=\"color: black\"><\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;margin: 0in 0in 0pt\"><span style=\"font-family: Consolas;color: blue;font-size: 10pt\">try<\/span><span style=\"font-family: Consolas;font-size: 10pt\"> { t.Wait(); }<\/span><\/p>\n<p class=\"MsoNormal\" style=\"line-height: normal;margin: 0in 0in 0pt\"><span style=\"font-family: Consolas;color: blue;font-size: 10pt\">catch<\/span><span style=\"font-family: Consolas;font-size: 10pt\"> (<span style=\"color: #2b91af\">AggregateException<\/span>) { <\/span><span style=\"font-family: Consolas;color: #2b91af;font-size: 9.5pt\">Console<\/span><span style=\"font-family: Consolas;font-size: 9.5pt\">.WriteLine(<span style=\"color: #a31515\">&#8220;gotcha!&#8221;<\/span>); <\/span><span style=\"font-family: Consolas;font-size: 10pt\">}<\/span><span style=\"font-family: Consolas;font-size: 9.5pt\"><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt\"><span style=\"font-size: small\"><span style=\"font-family: Calibri\">The issue has to do with the &ldquo;Just My Code&rdquo; 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).<span>&nbsp; <\/span>This is usually a good thing, because it allows you to pinpoint exactly where an exception is going unhandled.<span>&nbsp; <\/span>However, in this case, the Debugger is breaking before TPL can observe the exception.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt\"><span style=\"font-size: small\"><span style=\"font-family: Calibri\">Running without debugging or disabling &ldquo;Just My Code&rdquo; (Tools -&gt; Options -&gt; Debugging -&gt; General) should resolve the issue.<span>&nbsp; <\/span>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.<\/span><\/span><\/p>\n<p class=\"MsoNormal\" style=\"margin: 0in 0in 10pt\"><img decoding=\"async\" width=\"639\" height=\"178\" title=\"Just My Code Mode\" style=\"width: 639px;height: 178px\" alt=\"Just My Code Mode\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/21\/2010\/01\/just-my-code.png\" \/><\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/09\/94\/67\/36\/just-my-code.png\">just-my-code.png<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following code correctly observes and handles a Task exception and should print &ldquo;gotcha!&rdquo; to the console.&nbsp; By default though, the Debugger will report a crash.Task t = Task.Factory.StartNew(() =&gt; { throw new Exception(&#8220;poo&#8221;); }); try { t.Wait(); } catch (AggregateException) { Console.WriteLine(&#8220;gotcha!&#8221;); } The issue has to do with the &ldquo;Just My Code&rdquo; mode [&hellip;]<\/p>\n","protected":false},"author":485,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[7908],"tags":[7923,7909,7912],"class_list":["post-3091","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pfxteam","tag-faq","tag-parallel-extensions","tag-task-parallel-library"],"acf":[],"blog_post_summary":"<p>The following code correctly observes and handles a Task exception and should print &ldquo;gotcha!&rdquo; to the console.&nbsp; By default though, the Debugger will report a crash.Task t = Task.Factory.StartNew(() =&gt; { throw new Exception(&#8220;poo&#8221;); }); try { t.Wait(); } catch (AggregateException) { Console.WriteLine(&#8220;gotcha!&#8221;); } The issue has to do with the &ldquo;Just My Code&rdquo; mode [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3091","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/485"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=3091"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/3091\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=3091"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=3091"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=3091"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}