{"id":56,"date":"2012-10-11T15:35:40","date_gmt":"2012-10-11T13:35:40","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=56"},"modified":"2012-10-11T22:12:36","modified_gmt":"2012-10-11T20:12:36","slug":"abuse-of-try-except","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/11\/abuse-of-try-except\/","title":{"rendered":"(Ab)use of Try..Except"},"content":{"rendered":"<p>Today I found an example of the abuse of a try.. except block.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Try..Except\">\r\nprocedure TMyClass.DoSomething;\r\nvar\r\n  sValue: string;\r\n  iValue: Integer;\r\nbegin\r\n  ...\r\n  try\r\n    iValue := StrToInt(sValue);\r\n  except\r\n    iValue := 0;\r\n  end;\r\n  ...\r\nend;\r\n<\/pre>\n<p>The idea is simple. StrToInt converts a string to an integer as long as the string contains an integer value. If not an exception will be raised.<br \/>\nThis exception will be caught because of the try..except block and 0 will be assigned to the variable iValue.<\/p>\n<p>Please, never write code like this! The problem is that all kind of exceptions will be caught including EAccessViolation, EOuOfMemory etc..<br \/>\nCertainly is is better to use a function instead of StrToInt that doesn&#8217;t throws an exception but allows you to use a default value.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Try..Except\">\r\nprocedure TMyClass.DoSomething;\r\nvar\r\n  sValue: string;\r\n  iValue: Integer;\r\nbegin\r\n  ...\r\n  iValue := StrToIntDef(sValue, 0);\r\n  ...\r\nend;\r\n<\/pre>\n<p>If there is no other way, if you have to call a function that possibly raises an exception then catch only this particular exception. In our example we have to catch the EConvertError.<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Try..Except\">\r\nprocedure TMyClass.DoSomething;\r\nvar\r\n  sValue: string;\r\n  iValue: Integer;\r\nbegin\r\n  ...\r\n  try\r\n    iValue := StrToInt(sValue);\r\n  except\r\n    on E: EConvertError do\r\n      iValue := 0;\r\n  end;\r\n  ...\r\nend;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today I found an example of the abuse of a try.. except block. procedure TMyClass.DoSomething; var sValue: string; iValue: Integer; begin &#8230; try iValue := StrToInt(sValue); except iValue := 0; end; &#8230; end; The idea is simple. StrToInt converts a &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2012\/10\/11\/abuse-of-try-except\/\">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":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-tipsandtricks","tag-delphi"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/comments?post=56"}],"version-history":[{"count":5,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/56\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}