Handling Errors by Sticking Your Head in the Sand

You have seen that every call to a function that might throw an error must be marked with try and that any call with try must either be inside a do / catch block or inside a function that itself is marked with throws. These rules work together to make sure you are handling any potential errors. Try modifying your evaluate(_:) function to break one of these rules.

Listing 20.19  Modifying evaluate(_:) illegally

...
func evaluate(input: String) {
    print("Evaluating: \(input)")
    let lexer = Lexer(input: input)
    let tokens = try lexer.lex()

    do {
        let tokens = try lexer.lex() print("Lexer output: \(tokens)") let parser = Parser(tokens: tokens) let result = try parser.parse() print("Parser ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.