My Swift tip of the day…
Swift handles errors (what we would call exceptions in many other languages) using try and catch, but there’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 – we’re assuming this works fine) we create a defer (a bit like using a Disposable), then we useResource which might exception, but defer will now call closeResource for us
let someResource = openResource() defer { closeResource(someResource) } do { try useResource(someResource) } catch { throw MyError.resoureFailure() }