遵循的协议:Error
‘ 错误用遵循 Error
协议的类型的值来表示。这个空协议表明该类型可以用于错误处理
示例:
enum VendingMachineError: Error {
case invalidSelection //选择无效
case insufficientFunds(coinsNeeded: Int) //金额不足
case outOfStock //缺货
}
抛出错误:
throw VendingMachineError.insufficientFunds(coinsNeeded: 5)
处理错误的方式:
1、throwing函数传递错误:在函数声明的参数之后加上 throws
关键字
func canThrowErrors() throws -> String
struct Item {
var price: Int
var count: Int
}
class VendingMachine {
var inventory = [
"Candy Bar": Item(price: 12, count: 7),
"Chips": Item(price: 10, count: 4),
"Pretzels": Item(price: 7, count: 11)
]
var coinsDeposited = 0
func vend(itemNamed name: String) throws {
guard let item = inventory[name] else {
throw VendingMachineError.invalidSelection
}
guard item.count > 0 else {
throw VendingMachineError.outOfStock
}
guard item.price <= coinsDeposited else {
throw VendingMachineError.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
}
coinsDeposited -= item.price
var newItem = item
newItem.count -= 1
inventory[name] = newItem
print("Dispensing \(name)")
}
}
2、do-catch
语句
你可以使用一个 do-catch
语句运行一段闭包代码来处理错误。如果在 do
子句中的代码 抛出了一个错误,这个错误会与 catch
子句做匹配,从而决定哪条子句能处理它。
do {
try expression
statements
} catch pattern 1 {
statements
} catch pattern 2 where condition {
statements
} catch pattern 3, pattern 4 where condition {
statements
} catch {
statements
}
3、将错误作为可选类型处理
可以使用 try?
通过将错误转换成一个可选值来处理错误。如果是在计算 try?
表达式时抛出错误,该表达式的结果就为 nil
// 1
func someThrowingFunction() throws -> Int {
// ...
}
let x = try? someThrowingFunction()
let y: Int?
do {
y = try someThrowingFunction()
} catch {
y = nil
}
// 2
func fetchData() -> Data? {
if let data = try? fetchDataFromDisk() { return data }
if let data = try? fetchDataFromServer() { return data }
return nil
}
4、或者断言此错误根本不会发生
使用 defer
语句在即将离开当前代码块时执行一系列语句。该语句让你能执行一些必要的清理工作,不管是以何种方式离开当前代码块的——无论是由于抛出错误而离开,或是由于诸如 return
、break
的语句。
func processFile(filename: String) throws {
if exists(filename) {
let file = open(filename)
defer {
close(file)
}
while let line = try file.readline() {
// 处理文件。
}
// close(file) 会在这里被调用,即作用域的最后。
}
}