-
Book Overview & Buying
-
Table Of Contents
Mastering Swift 6 - Seventh Edition
By :
In previous versions of Swift, functions could only declare that they would throw errors without specifying the error type(s). This led to less precise error handling, especially with frameworks, because the receiving function did not know the error types that could be thrown. Now, with typed throws, you can define functions that throw specific error types.
Let’s look at how typed throws work by looking at the addPlayer() function that we defined in our BaseballTeam type earlier in the chapter. The addPlayer() function was previously defined like this;
mutating func addPlayer(player: BaseballPlayer) throws { }
With this function definition, we were unaware of what types of errors could be thrown. With typed throws, we can define this function like this:
mutating func addPlayer(player: BaseballPlayer) throws(PlayerNumberError) { }
Typed throws are not required, and untyped throws can still be defined. The following shows the three ways functions...