Add Section About Error Handling (#33365)

* Add Section About Error Handling

* fix: added front matter block
This commit is contained in:
Ishan Chhabra
2019-01-27 08:16:41 +05:30
committed by Randell Dawson
parent c64a6c6dfa
commit 9359b1aa36

View File

@ -0,0 +1,37 @@
---
title: Error Handling
---
# Error Handling
You represent errors using any type that adopts the `Error` protocol.
```Swift
enum PrinterError: Error {
case outOfPaper
case noToner
case onFire
}
```
Use `throw` to throw an error and `throws` to mark a function that can throw an error. If you throw an error in a function, the function returns immediately and the code that called the function handles the error.
```swift
func send(job: Int, toPrinter printerName: String) throws -> String {
if printerName == "Never Has Toner" {
throw PrinterError.noToner
}
return "Job sent"
}
```
There are several ways to handle errors. One way is to use `do`-`catch`. Inside the `do`block, you mark code that can throw an error by writing `try` in front of it. Inside the `catch` block, the error is automatically given the name `error` unless you give it a different name.
```swift
do {
let printerResponse = try send(job: 1040, toPrinter: "Bi Sheng")
print(printerResponse)
} catch {
print(error)
}
// Prints "Job sent"
```