November 2016
Intermediate to advanced
480 pages
14h 42m
English
To handle errors, Swift uses a control construct you have not yet seen: do/catch, with at least one try statement inside of the do.
We will explain in a moment.
First, modify evaluate(_:) to use this control flow to handle errors coming from lex().
Listing 20.13 Error handling in evaluate(_:)
...
func evaluate(_ input: String) {
print("Evaluating: \(input)")
let lexer = Lexer(input: input)
do {
let tokens = try lexer.lex()
print("Lexer output: \(tokens)")
} catch {
print("An error occurred: \(error)")
}
}
What do all these new keywords mean?
do introduces a new scope, much like an if statement.
Inside of the do scope, you can write code as normal, like calling print(). In addition, you can call functions ...
Read now
Unlock full access