O'Reilly logo

Swift Programming: The Big Nerd Ranch Guide by John Gallagher, Matthew Mathias

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.21 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required