{"id":110,"date":"2010-08-25T13:20:45","date_gmt":"2010-08-25T12:20:45","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=110"},"modified":"2010-08-25T13:20:45","modified_gmt":"2010-08-25T12:20:45","slug":"try-catch-mistakes","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2010\/08\/25\/try-catch-mistakes\/","title":{"rendered":"try catch mistakes"},"content":{"rendered":"<p>To cope with errors in code we can do many thing, among many approaches the most powerful is to use &#8220;try catch finally&#8221; block around the code that might cause trouble (one we know that it throws Errors) but what I have noticed working with different programmers common mistake is to use &#8220;try catch&#8221; block as a filter of errors.<br \/>\n<!--more--><\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   willCauseTrouble();\r\n}\r\ncatch(e:*)\r\n{\r\n}\r\n<\/pre>\n<p>This is just asking for trouble we have no way to cope with that, if you did use the &#8220;try catch&#8221;, at least use it to report errors:<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   willCauseTrouble();\r\n}\r\ncatch(e:*)\r\n{\r\n   trace(&quot;error occurred\\n&quot;+(e as Error).getStackTrace());\r\n}\r\n<\/pre>\n<p>next thing is that instead of isolating the code that can cause trouble programmer wraps entire block of code within &#8220;try catch&#8221; statement.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/**\r\n * Dummy method to simulate error throwing\r\n *\/\r\nfunction problematicCode():void\r\n{\r\n      throw new Error(&quot;Error&quot;);\r\n}\r\n\r\n\r\n\r\ntry\r\n{\r\n      trace(&quot;action 1&quot;);\r\n      problematicCode();\r\n      trace(&quot;this action will not be executed&quot;)\r\n}\r\ncatch(e:*)\r\n{\r\n      trace(&quot;I have caught an error&quot;);\r\n}\r\n<\/pre>\n<p>above snippet pictures this perfectly, the second trace is not executed, can you see my point, by wrapping entire block of code in the try we are stopping further execution, and it is a show-stopper. You should always try to predict which piece of code might be problematic, and &#8220;try catch&#8221; only it.<\/p>\n<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/-----------------\r\ntrace(&quot;action 1&quot;);\r\ntry\r\n{\r\n      \/\/isolated problem\r\n      problematicCode();\r\n}\r\ncatch(e:*)\r\n{\r\n      trace(&quot;I have caught an error&quot;);\r\n}\r\ntrace(&quot;this action WILL BE executed&quot;)\r\n\r\n<\/pre>\n<p>And as last word, this is debug utility not perfect hideout:) if your code throws errors, it can still work, but you should fix the cause, always.<\/p>\n<p>happy coding<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To cope with errors in code we can do many thing, among many approaches the most powerful is to use &#8220;try catch finally&#8221; block around the code that might cause trouble (one we know that it throws Errors) but what &hellip; <a href=\"https:\/\/greladesign.co\/blog\/2010\/08\/25\/try-catch-mistakes\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[17,9],"tags":[12,21,20],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/110"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=110"}],"version-history":[{"count":3,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions"}],"predecessor-version":[{"id":113,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/110\/revisions\/113"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}