Representing errors

Before we can really understand how error handling works in Swift, we must see how we would represent an error. In Swift, errors are represented by values of types that conform to the Error protocol. Swift's enumerations are very well suited to modeling error conditions because we have a finite number of error conditions to represent.

Let's look at how we would use an enumeration to represent an error. For this, we will define a fictitious error named MyError with three error conditions: Minor, Bad, and Terrible:

enum MyError: Error { 
  case Minor 
  case Bad 
  case Terrible 
} 

In this example, we define that the MyError enumeration conforms to the Error protocol. We then define the three error conditions: Minor, Bad, and Terrible. That is all there is to defining a basic error condition.

We can also use the associated values with our error conditions to allow us to add more details about the error condition. Let's say that we want to add a description to the Terrible error condition. We would do it like this:

enum MyError: Error { 
  case Minor 
  case Bad 
  case Terrible(description:String) 
} 

Those who are familiar with exception handling in Java and C# can see that representing errors in Swift is a lot cleaner and easier, because we do not need to create a lot of boilerplate code or create a full class. With Swift, it can be as simple as defining an enumeration with our error conditions. Another advantage that we have is that it is very easy to define multiple error conditions and group them together so that all related error conditions are of one type.

Now let's see how we can model errors in Swift. For this example, let's look at how we would assign numbers to players on a baseball team. For a baseball team, every new player who is called up is assigned a unique number. This number must also be within a certain range, because only two numbers fit on a baseball jersey. Therefore, we would have three error conditions: number is too large, number is too small, and number is not unique. The following example shows how we might represent these error conditions:

enum PlayerNumberError: Error { 
  case NumberTooHigh(description: String) 
  case NumberTooLow(description: String) 
  case NumberAlreadyAssigned 
} 

With the PlayerNumberError type, we define three very specific error conditions that tell us exactly what went wrong. These error conditions are also grouped together in one type since they are all related to assigning the players' numbers.

This method of defining errors allows us to define very specific errors that let our code know exactly what went wrong if an error condition occurs. It also lets us group the errors so that all related errors can be defined in the same type.

Now that we know how to represent errors, let's look at how we would throw errors.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset