Handling errors with try, throw, do, and catch
Errors happen during programming. These errors may be due to your own code behaving in unexpected ways, or due to unexpected information or behavior from external systems. When these errors happen, it’s important to handle them appropriately. Good error handling can separate a good app from a great app.
Swift provides a deliberate and flexible pattern to handle errors, allowing specific errors to be cascaded through a complex system.
In this recipe, we will discover how to define errors and throw them when necessary.
Getting ready
In this recipe, we won’t be using any components from the previous recipes, so you can create a new playground for this recipe.
How to do it...
To examine error handling, we will model a process that can go wrong, and for me, that is cooking a meal:
- First, let’s define the steps involved in cooking a meal as states that the meal will transition through:
enum MealState...