{"id":36143,"date":"2005-03-17T07:03:00","date_gmt":"2005-03-17T07:03:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/03\/17\/your-exception-handler-can-encounter-an-exception\/"},"modified":"2005-03-17T07:03:00","modified_gmt":"2005-03-17T07:03:00","slug":"your-exception-handler-can-encounter-an-exception","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050317-00\/?p=36143","title":{"rendered":"Your exception handler can encounter an exception"},"content":{"rendered":"<p>\nConsider the following code,\nwritten in C# just for kicks; the problem is generic to any\nenvironment that supports exception handling.\n<\/p>\n<pre>\nvoid ObliterateDocument()\n{\n try {\n  try {\n   document.DestroyAll();\n  } finally {\n   document.Close();\n   document.DestroyExtensions();\n   document.DestroyPlugins();\n  }\n } finally {\n  document.Destroy();\n }\n}\n<\/pre>\n<p>\nSome time later, you find yourself facing an assertion\nfailure from <code>document.Destroy()<\/code> claiming that you are\ndestroying the document while there are still active plugins.\nBut there is your call to <code>document.DestroyPlugins()<\/code>,\nand it&#8217;s in a <code>finally<\/code> block, and the whole point\nof a <code>finally<\/code> block is that there is no way you can\nescape without executing it.\n<\/p>\n<p>\nSo why didn&#8217;t <code>document.DestroyPlugins()<\/code> execute?\n<\/p>\n<p>\nBecause your exception handler itself encountered an exception.\n<\/p>\n<p>\nThe exception handler is not active during its own\n<code>finally<\/code> clause.\nAs a result,\nif an exception is thrown during <code>document.Close()<\/code>,\nthe exception handler search begins at the block\n<strong>outside<\/strong> the <code>finally<\/code> block.\n<\/p>\n<p>\n(That the exception handler is not active during its own\n<code>finally<\/code> clause should be obvious.  It would mean\nthat if an exception were to occur during the <code>finally<\/code>\nclause, the program would go into an infinite loop.  And it also\nwouldn&#8217;t be possible to rethrow a caught exception; your <code>throw<\/code>\nwould end up caught by yourself!)\n<\/p>\n<p>\nIn this case, the exception was caught by some outer caller,\ncausing the remainder of the first <code>finally<\/code>\nblock to be abandoned.  The other <code>finally<\/code> blocks\ndo run since they contain the one that died.\n<\/p>\n<p>\n(This bug also exists in the\n<a HREF=\"\/oldnewthing\/archive\/2005\/01\/14\/352949.aspx#353092\">\nproposed alternative to error-checking code<\/a> posted by\nan anonymous commenter.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Consider the following code, written in C# just for kicks; the problem is generic to any environment that supports exception handling. void ObliterateDocument() { try { try { document.DestroyAll(); } finally { document.Close(); document.DestroyExtensions(); document.DestroyPlugins(); } } finally { document.Destroy(); } } Some time later, you find yourself facing an assertion failure from document.Destroy() claiming [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-36143","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Consider the following code, written in C# just for kicks; the problem is generic to any environment that supports exception handling. void ObliterateDocument() { try { try { document.DestroyAll(); } finally { document.Close(); document.DestroyExtensions(); document.DestroyPlugins(); } } finally { document.Destroy(); } } Some time later, you find yourself facing an assertion failure from document.Destroy() claiming [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=36143"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36143\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=36143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}