{"id":9335,"date":"2022-10-09T11:34:09","date_gmt":"2022-10-09T11:34:09","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9335"},"modified":"2022-10-09T11:34:09","modified_gmt":"2022-10-09T11:34:09","slug":"try-catch-but-no-finally-in-swift","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/try-catch-but-no-finally-in-swift\/","title":{"rendered":"try&#8230;catch, but no finally in Swift"},"content":{"rendered":"<p><em>My Swift tip of the day&#8230;<\/em><\/p>\n<p>Swift handles errors (what we would call exceptions in many other languages) using <em>try<\/em> and <em>catch<\/em>, but there&#8217;s no <em>finally<\/em> keyword. Instead, we can wrap a closure and pass to the <em>defer<\/em> function instead. For example we open some resource (file or whatever &#8211; we&#8217;re assuming this works fine) we create a defer (a bit like using a Disposable), then we <em>useResource<\/em> which might exception, but defer will now call <em>closeResource<\/em> for us<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nlet someResource = openResource()\r\ndefer { closeResource(someResource) }\r\n\r\ndo {\r\n   try useResource(someResource)\r\n} catch {\r\n   throw MyError.resoureFailure()\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>My Swift tip of the day&#8230; Swift handles errors (what we would call exceptions in many other languages) using try and catch, but there&#8217;s no finally keyword. Instead, we can wrap a closure and pass to the defer function instead. For example we open some resource (file or whatever &#8211; we&#8217;re assuming this works fine) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9335","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9335"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9335\/revisions"}],"predecessor-version":[{"id":9491,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9335\/revisions\/9491"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}