Lesson 017 – Revisiting Error Handling for Lesson 015


Back in Lesson 015, we made a simple call to a REST API. At the time, we cheated in handling one of the errors that could occur. Take a look at this line from Lesson 015 to remind you of what we did

Better Error Handling

Now that we have Lesson 016 under our belt, we know better how to handle that situation now. Instead of the try!, which tells Swift that we don’t expect an error at all. We are so confident that we’ve said, “even if we get an error, just blow up the program… life isn’t worth living”. Those are bold statements considering we are dealing with fickle network connections.

Here is the complete program with the proper error handling in place

Now, we’ve exchanged our try! for a try and wrapped that segment of code in a do…catch block. If you want to test this out, you can use these different URLs to force each of our possible error conditions.


Doing error handling in a situation like a network call is often a “worst case scenario”. I realize that it may seem like we did a lot of protecting and if and else and try, but you can see that we have at least 4 different ways that this could have failed. Often, our code is going to be much more straightforward, so don’t judge too harshly by this example 😉

Add Comment

Required fields are marked *. Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.