Chapter    16

Error Handling

What is error handling? It is a process of properly handling unusual conditions in the program. There is an old saying: Garbage in, garbage out. Computers are machines that are very good at processing commands without questioning the input that is given to it. So if you give them bad data (garbage data), they will process it and give you bad results (garbage out). So it is the programmer’s responsibility to put guards against invalid or erroneous situations. Swift provides first-class support for handling these situations.

Let’s take a situation of downloading data from a remote server. If everything went correct, you would have data and then you could process it, but there can be various errors:

  • Network not reachable ...

Get Learn Swift 2 on the Mac, Second Edition 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.