{"id":540,"date":"2017-04-05T02:38:20","date_gmt":"2017-04-05T00:38:20","guid":{"rendered":"http:\/\/blog.kassebaum.eu\/?p=540"},"modified":"2017-04-05T02:38:20","modified_gmt":"2017-04-05T00:38:20","slug":"nice-approach-for-exception-safe-code","status":"publish","type":"post","link":"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/05\/nice-approach-for-exception-safe-code\/","title":{"rendered":"Nice Approach For Exception Safe Code"},"content":{"rendered":"<p>Maybe everyone knows the issue with code that is not exception safe. Let&#8217;s see an example:<\/p>\n<pre class=\"lang:delphi decode:true\" title=\"Possible Memory Leak Inn Case Of An Exception\">function MyFunction: TMyObject;\r\nbegin\r\n  Result := TMyObject,Create;\r\n  Result.DoSomething;\r\nend;<\/pre>\n<p>The function MyFunction returns an instance of an object which is normally no problem. But MyFunction creates the instance and then runs further code. And in case of an exception no one &#8220;cares&#8221; about the instance and you will simply get a memory leak.<\/p>\n<p>That&#8217;s why people normally avoid functions that return instances or write the code in an exception safe way:<\/p>\n<pre class=\"lang:delphi decode:true\">function MyFunction: TMyObject;\r\nbegin\r\n  Result := TMyObject,Create;\r\n  try\r\n    Result.DoSomething;\r\n  except\r\n    on E: Exception do\r\n    begin\r\n      Result.Free;\r\n      raise;\r\n    end;\r\n  end;\r\nend;<\/pre>\n<p>At this point some purists would say that the Result should also be assigned to nil after it had been destroyed.<\/p>\n<pre class=\"lang:delphi decode:true\">function MyFunction: TMyObject;\r\nbegin\r\n  Result := TMyObject,Create;\r\n  try\r\n    Result.DoSomething;\r\n  except\r\n    on E: Exception do\r\n    begin\r\n      FreeAndNil(Result);\r\n      raise;\r\n    end;\r\n  end;\r\nend;<\/pre>\n<p>I don&#8217;t want to start a long discussion about FreeAndNil but what people, including myself, don&#8217;t like is to catch an exception in order to raise it again a moment later.<\/p>\n<p>Instead the code could be written with a try-finally-block:<\/p>\n<pre class=\"lang:delphi decode:true \">function MyFunction: TMyObject;\r\nvar\r\n  lMyObject: TMyObject;\r\nbegin\r\n  lMyObject := TMyObject,Create;\r\n  try\r\n    lMyObject.DoSomething;\r\n    Result := lMyObject;\r\n    lMyObject := nil;\r\n  finally\r\n    lMyObject.Free;\r\n  end;\r\nend;<\/pre>\n<p>The tricks are:<\/p>\n<ol>\n<li>Free can be called if an instance is nil.<\/li>\n<li>Finally is always called.<\/li>\n<li>After assigning the Result no further code that could cause an exception is called.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Maybe everyone knows the issue with code that is not exception safe. Let&#8217;s see an example: function MyFunction: TMyObject; begin Result := TMyObject,Create; Result.DoSomething; end; The function MyFunction returns an instance of an object which is normally no problem. But &hellip; <a href=\"https:\/\/www.kassebaum.eu\/blog\/2017\/04\/05\/nice-approach-for-exception-safe-code\/\">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":[31,12,32,34,35,13,21,23,27,3],"tags":[],"class_list":["post-540","post","type-post","status-publish","format-standard","hentry","category-delphi","category-firemonkey","category-rad-studio-10-seattle","category-rad-studio-10-1-berlin","category-rad-studio-10-2-tokyo","category-rad-studio-xe5","category-rad-studio-xe6","category-rad-studio-xe7","category-rad-studio-xe8","category-tipsandtricks"],"_links":{"self":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/540","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=540"}],"version-history":[{"count":6,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/540\/revisions"}],"predecessor-version":[{"id":546,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/posts\/540\/revisions\/546"}],"wp:attachment":[{"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/media?parent=540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/categories?post=540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kassebaum.eu\/blog\/wp-json\/wp\/v2\/tags?post=540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}