{"id":3240,"date":"2011-02-09T16:46:00","date_gmt":"2011-02-09T16:46:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/subterranean-il-fault-exception-handlers\/"},"modified":"2021-04-29T15:30:54","modified_gmt":"2021-04-29T15:30:54","slug":"subterranean-il-fault-exception-handlers","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/subterranean-il-fault-exception-handlers\/","title":{"rendered":"Subterranean IL: Fault exception handlers"},"content":{"rendered":"<p><code>Fault<\/code> event handlers are one of the two handler types that aren&#8217;t available in C#. It behaves exactly like a <code>finally<\/code>, except it is only run if control flow exits the block due to an exception being thrown.<\/p>\n<p>As an example, take the following method: <\/p>\n<pre>.method public static void FaultExample(bool throwException) {\n    .try {\n        ldstr \"Entering try block\"\n        call void [mscorlib]System.Console::WriteLine(string)\n        ldarg.0\n        brfalse.s NormalReturn\n         \n      ThrowException:\n        ldstr \"Throwing exception\"\n        call void [mscorlib]System.Console::WriteLine(string)\n        newobj void [mscorlib]System.Exception::.ctor()\n        throw\n         \n      NormalReturn:\n        ldstr \"Leaving try block\"\n        call void [mscorlib]System.Console::WriteLine(string)\n        leave.s Return\n    }\n    fault {\n        ldstr \"Fault handler\"\n        call void [mscorlib]System.Console::WriteLine(string)\n        endfault\n    }\n    \n  Return:\n    ldstr \"Returning from method\"\n    call void [mscorlib]System.Console::WriteLine(string)    \n    ret\n}<\/pre>\n<p>If we pass <code>true<\/code> to this method the following gets printed: <\/p>\n<pre>Entering try block\nThrowing exception\nFault handler<\/pre>\n<p> and the exception gets passed up the call stack. So, the exception gets thrown, the <code>fault<\/code> handler gets run, and the exception propagates up the stack afterwards in the normal way.<\/p>\n<p>If we pass <code>false<\/code>, we get the following: <\/p>\n<pre>Entering try block\nLeaving try block\nReturning from method<\/pre>\n<p> Because we are leaving the <code>.try<\/code> using a <code>leave.s<\/code> instruction, and not throwing an exception, the <code>fault<\/code> handler does <em>not<\/em> get called.  <\/p>\n<p><b>Fault handlers and C#<\/b><\/p>\n<p>So why were these not included in C#? It seems a pretty simple feature; one extra keyword that compiles in exactly the same way, and with the same semantics, as a <code>finally<\/code> handler. If you think about it, the same behaviour can be replicated using a normal <code>catch<\/code> block: <\/p>\n<pre>try {\n    throw new Exception();\n}\ncatch {\n    \/\/ fault code goes here\n    throw;\n}<\/pre>\n<p>The catch block only gets run if an exception is thrown, and the exception gets rethrown and propagates up the call stack afterwards; exactly like a <code>fault<\/code> block. The only complications that occur is when you want to add a fault handler to a try block with existing catch handlers. Then, you either have to wrap the <code>try<\/code> in another <code>try<\/code>: <\/p>\n<pre>try {\n    try {\n        \/\/ ...\n    }\n    catch (DirectoryNotFoundException) {\n        \/\/ ...\n        \/\/ leave.s as normal...\n    }\n    catch (IOException) {\n        \/\/ ...\n        throw;\n    }\n}\ncatch {\n    \/\/ fault logic\n    throw;\n}<\/pre>\n<p> or separate out the fault logic into another method and call that from the appropriate handlers: <\/p>\n<pre>try {\n    \/\/ ...\n}\ncatch (DirectoryNotFoundException ) {\n    \/\/ ...\n}\ncatch (IOException ioe) {\n    \/\/ ...\n    HandleFaultLogic();\n    throw;\n}\ncatch (Exception e) {\n    HandleFaultLogic();\n    throw;\n}<\/pre>\n<p>To be fair, the number of times that I would have found a <code>fault<\/code> handler useful is minimal. Still, it&#8217;s quite annoying knowing such functionality exists, but you&#8217;re not able to access it from C#. Fortunately, there are some easy workarounds one can use instead.<\/p>\n<p><a href=\"https:\/\/www.simple-talk.com\/community\/blogs\/simonc\/archive\/2011\/02\/14\/99399.aspx\">Next time<\/a>: filter handlers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fault event handlers are one of the two handler types that aren&#8217;t available in C#. It behaves exactly like a finally, except it is only run if control flow exits the block due to an exception being thrown. As an example, take the following method: .method public static void FaultExample(bool throwException) { .try { ldstr&#8230;&hellip;<\/p>\n","protected":false},"author":186659,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[143538,2],"tags":[],"coauthors":[],"class_list":["post-3240","post","type-post","status-publish","format-standard","hentry","category-dotnet-development","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/186659"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3240"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3240\/revisions"}],"predecessor-version":[{"id":75605,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3240\/revisions\/75605"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3240"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}