{"id":351,"date":"2015-07-23T06:21:22","date_gmt":"2015-07-23T06:21:22","guid":{"rendered":"http:\/\/www.exploreax.com\/blog\/?p=351"},"modified":"2015-07-23T06:21:22","modified_gmt":"2015-07-23T06:21:22","slug":"beware-the-catch","status":"publish","type":"post","link":"http:\/\/www.exploreax.com\/blog\/blog\/2015\/07\/23\/beware-the-catch\/","title":{"rendered":"Beware the &#8220;Catch&#8221;"},"content":{"rendered":"<p>Recently while debugging some legacy code I came across the following interesting observation regarding transaction scopes (ttsbegin, ttscommit) and the exception handling surrounding it.\u00a0While walking the legacy\u00a0code was observing\u00a0very strange and unexpected behaviour and after some online investigation i found the following statement from Microsoft:<\/p>\n<p><em>&#8220;When an exception is thrown inside a <span class=\"input\">ttsBegin<\/span> &#8211; <span class=\"input\">ttsCommit<\/span> transaction block, no <span class=\"input\">catch<\/span> statement inside that transaction block can process the exception. Instead, the innermost <span class=\"input\">catch<\/span> statements that are outside the transaction block are the first <span class=\"input\">catch<\/span> statements to be tested.&#8221;<br \/>\n(<\/em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/aa893385.aspx\">https:\/\/msdn.microsoft.com\/en-us\/library\/aa893385.aspx<\/a>)<\/p>\n<p>In its simplest form this means that for the following code:<\/p>\n<pre class=\"lang:default decode:true \"> try\r\n    {\r\n        ttsBegin;\r\n        try\r\n        {\r\n            throw error(\"Thrown Error\");\r\n        }\r\n        catch (Exception::Error)\r\n        {\r\n            error(\"Inside Error\");\r\n        }\r\n        ttsCommit;\r\n    }\r\n    catch (Exception::Error)\r\n    {\r\n        error(\"Outside Error\");\r\n    }<\/pre>\n<p>Prints the following results:<br \/>\n<a href=\"http:\/\/www.exploreax.com\/blog\/wp-content\/uploads\/2015\/07\/2015-07-23_0801.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-352\" src=\"http:\/\/www.exploreax.com\/blog\/wp-content\/uploads\/2015\/07\/2015-07-23_0801.png\" alt=\"2015-07-23_0801\" width=\"351\" height=\"437\" srcset=\"http:\/\/www.exploreax.com\/blog\/wp-content\/uploads\/2015\/07\/2015-07-23_0801.png 351w, http:\/\/www.exploreax.com\/blog\/wp-content\/uploads\/2015\/07\/2015-07-23_0801-241x300.png 241w\" sizes=\"(max-width: 351px) 100vw, 351px\" \/><\/a><\/p>\n<p>Now when it comes to a simple block of code like above one may say &#8220;Well that is simply silly coding&#8221;, however it becomes harder to anticipate results when that inner try&#8230;catch is within a method somewhere deeper inside the call stack (perhaps in standard code somewhere) e.g.<\/p>\n<pre class=\"lang:default decode:true\">    void innerFunction()\r\n    {\r\n        try\r\n        {\r\n            ttsbegin;\r\n            throw error(\"Thrown Error\");\r\n            ttscommit;\r\n        }\r\n        catch (Exception::Error)\r\n        {\r\n            error(\"Inside Error\");\r\n        }\r\n    }\r\n    \r\n    try\r\n    {\r\n        ttsBegin;\r\n            innerFunction();\r\n        ttsCommit;\r\n    }\r\n    catch (Exception::Error)\r\n    {\r\n        error(\"Outside Error\");\r\n    }<\/pre>\n<p>Further more if your inner exception does cleanups or logging of information this will not happen by carelessly adding ttsBegins and ttsCommits around the calling code E.G. &#8220;writeErrorLog&#8221; will never be called in the following function regardless of what\u00a0the writer of &#8220;innerFunction&#8221; does if the writer of the caller adds TTSBEGIN AND COMMIT<\/p>\n<pre class=\"lang:default decode:true\">static void testTransactions(Args _args)\r\n{\r\n    void innerFunction()\r\n    {\r\n        while forUpdate select myTable\r\n        {\r\n            try\r\n            {\r\n            \r\n                ttsBegin;\r\n                if (someCondition)\r\n                {\r\n                    throw error(\"Throw Error\");\r\n                }\r\n                else\r\n                {\r\n                    myTable.myField = \"update\";\r\n                    myTable.update();\r\n                }\r\n                ttsCommit;\r\n            }\r\n            catch (Exception::Error)\r\n            {\r\n                writeErrorLog(strFmt(\"Failed on record %1\", myTable.RecId));\r\n                error(\"Inside Error\");\r\n            }\r\n        }\r\n    }\r\n    \r\n    try\r\n    {\r\n        ttsBegin;\r\n            innerFunction();\r\n        ttsCommit;\r\n    }\r\n    catch (Exception::Error)\r\n    {\r\n        error(\"Outside Error\");\r\n    }\r\n}<\/pre>\n<p>I thought I would just paste these observations for anyone who like me has experienced this type of &#8220;strange behavior&#8221; in the past and didn&#8217;t know the exact reason and also as a warning to beware of the &#8220;Catch&#8221;.<\/p>\n<p>Feel free to share your comments, observations and thoughts.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently while debugging some legacy code I came across the following interesting observation regarding transaction scopes (ttsbegin, ttscommit) and the exception handling surrounding it.\u00a0While walking the legacy\u00a0code was observing\u00a0very strange and unexpected behaviour and after some online investigation i found the following statement from Microsoft: &#8220;When an exception is thrown inside a ttsBegin &#8211; ttsCommit&hellip;<a href=\"http:\/\/www.exploreax.com\/blog\/blog\/2015\/07\/23\/beware-the-catch\/\">Read more <span class=\"screen-reader-text\">Beware the &#8220;Catch&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,6,5,4],"tags":[44,92,91,93,94],"_links":{"self":[{"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/posts\/351"}],"collection":[{"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":1,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"predecessor-version":[{"id":353,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/posts\/351\/revisions\/353"}],"wp:attachment":[{"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.exploreax.com\/blog\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}