{"id":1227,"date":"2013-11-15T10:13:46","date_gmt":"2013-11-15T08:13:46","guid":{"rendered":"http:\/\/hen.co.za\/blog\/?p=1227"},"modified":"2013-11-15T10:13:46","modified_gmt":"2013-11-15T08:13:46","slug":"should-i-handle-exceptions-or-not","status":"publish","type":"post","link":"https:\/\/hen.co.za\/blog\/2013\/11\/should-i-handle-exceptions-or-not\/","title":{"rendered":"Should I handle Exceptions or not?"},"content":{"rendered":"<p>I&#8217;ll start this post with a statement or answer that might seem controversial.<\/p>\n<p><strong>The answer is &#8216;NO&#8217;<\/strong><\/p>\n<p>Why? Let me explain. If your code has reached a state where an Exception has been thrown it means you have already missed something (possible) important that should have been addressed. Sure, throwing (excuse the pun) a &#8216;try&#8230; catch&#8217; around every block of code is nice and all &#8211; and you should do it anyway (for other reasons) but it is no excuse for writing bad code.<\/p>\n<p><strong>More details<\/strong><\/p>\n<p>One reason I say these things I recently (re)discovered myself&#8230; again is that &#8216;Performance&#8217; in an application can greatly be influenced by exceptions since the whole process of encountering\/throwing an exception in code is a slow thing. When you have code that relies on catching an exception in order to handle an (possibly) expected condition of some variables it is a sign that you are doing it wrong&#8230; It may not be a big issue if the affected code is called only once in a blue moon but once it is in a section that is called repeatedly and is expected to perform at speed it becomes a huge issue.<\/p>\n<p>Have a look at this code. It will run fine but and actually be usable but&#8230;<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\ntry\r\n{\r\n   SomeType tp = SomeObject.SomeVar;\r\n}\r\ncatch \/\/ null reference exception or something expected the first time\r\n{\r\n  SomeObject = new SomeObjectType();\r\n  ...\r\n}\r\n<\/pre>\n<p>Now look at a better way<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nif (SomeObject == null)\r\n\r\nSomeObject = new SomeObjectType();\r\n\r\nSomeType tp = SomeObject.SomeVar;\r\n\r\n...\r\n<\/pre>\n<p>This code will actually run faster and does not rely on an Exception to handle something that should be done right to start with.<\/p>\n<p>Ok, there you have it&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ll start this post with a statement or answer that might seem controversial. The answer is &#8216;NO&#8217; Why? Let me explain. If your code has reached a state where an Exception has been thrown it means you have already missed &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/hen.co.za\/blog\/2013\/11\/should-i-handle-exceptions-or-not\/\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[7,8,9,303,24],"class_list":["post-1227","post","type-post","status-publish","format-standard","hentry","category-development","tag-net","tag-c","tag-code","tag-development","tag-tip"],"_links":{"self":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/comments?post=1227"}],"version-history":[{"count":2,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions"}],"predecessor-version":[{"id":1229,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/posts\/1227\/revisions\/1229"}],"wp:attachment":[{"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/media?parent=1227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/categories?post=1227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hen.co.za\/blog\/wp-json\/wp\/v2\/tags?post=1227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}