Swift’s guard and throw statements encourage a style of programming where we return early from a given scope in case of error. Returning early from a given scope poses it’s own problems though – primarily how to ensure that any resources that were created within the scope are reliably cleaned up before exiting. In this post, we look at how to solve these types of a problem using Swift’s defer
Statement.
Error Handling in Swift
As developers we have complete control of the code we write and the features we include, but despite our best efforts, things can go wrong. In this post we look at how to handle these situations by taking a look at error handling in Swift.