We ended the last section by examining some sample code after it had been made production-quality by adding correct error handling techniques. In this section, we'll dig into the most common Swift error handling techniques, which will help ensure all the code you develop in Swift will be robust and of high quality.

Swift supports many of the same error handling techniques available in other object-oriented languages, such as C++, Java, and C#. Functions—either your own or standard library functions—often return error codes as integers, error types, and Boolean variables. In addition, Swift provides exception handling using the do…catch construction, which is functionally equivalent to the try…catch construction used in many other languages.

The guard statement is most commonly used at the top of a function body to validate that the data the function will use to complete its task is in an expected state. In this sense, the guard statement acts as a guard at the gate—checking the contents of inputs to the function before they're allowed in.

In early versions of Swift, we didn't have the guard statement, and it was common to implement functions structured like the following:

While this function isn't too difficult to follow, it can become confusing for the reader where the ending brace of the if let { } block ends. Developers would frequently reduce the editor font to a tiny size to try to make out where in the sequence of ending braces the close of the original error checking if let block ended!

The guard keyword is effectively a clearer version of this structure—moving the closing braces of validations together in neat code blocks. An equivalent function using the guard syntax is as follows:

In the second version, the guard statement makes the code more readable, and moves all the state-checking code to the beginning of the function where it can be easily reviewed and understood.

We have reached the end of this section. Here, we focused on error handling and exception handling, as implemented in Swift. To reiterate, Swift uses do…catch instead of try…catch and also allows us to use multiple catch blocks.

Exception handling, as the name implies, is an error handling technique that enables you to let the Swift compiler know what errors you expect, and provide a way to listen for them if they occur while your program is running. We'll now apply exception handling in one of the most common use cases for application developers—parsing data structures from JSON into application data structures.

Use an Xcode playground to practice catching an exception while parsing a JSON string into a custom data structure—a very common task in any application development work that involves integration with web services.

Because the jsonText data is not in the correct format (the name field cannot begin with an uppercase letter), the decoder.decode function throws an exception. The exception is caught in the catch block, reporting an error. You eliminate the exception by changing the case of the name field in the jsonText string.