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

f​u​n​c​ ​e​v​a​l​u​a​t​e​(​i​n​p​u​t​:​ ​S​t​r​i​n​g​)​ ​{​
 ​ ​ ​ ​p​r​i​n​t​(​"​E​v​a​l​u​a​t​i​n​g​:​ ​\​(​i​n​p​u​t​)​"​)​
 ​ ​ ​ ​l​e​t​ ​l​e​x​e​r​ ​=​ ​L​e​x​e​r​(​i​n​p​u​t​:​ ​i​n​p​u​t​)​
 ​ ​ ​ ​l​e​t​ ​t​o​k​e​n​s​ ​=​ ​t​r​y​ ​l​e​x​e​r​.​l​e​x​(​)​ ​ ​ ...

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

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.