Swift(十八、错误处理)
1、Swift入门学习笔记(第一版),对Swift的基础知识点进行梳理总结。知识点一直在变,只是作为参考,以苹果官方文档为准~ 2、在学习完基本的知识点以后会结合官方文档及相关资料,在此版本的基础上进行添加更改。 十八、错误处理响应错误以及从错误中恢复的过程,在运行时可恢复错误抛出,捕获,传送和操作的高级支持 1、表示并抛出错误错误遵循 enum VendingMachineError: ErrorType {
case InvalidSelection //选择无效
case InsufficientFunds(coinsNeeded: Int) //金额不足
case OutOfStock //缺货
}
使用throw关键字抛出 throw VendingMachineError.OutOfStock
2、处理错误Swift提供四种方式: b、用do-catch语句处理错误 c、将错误作为可选类型处理 d、断言此错误根本不会发生 2.1、用throwing函数传递错误在函数参数列表之后, func canThrowsErrors() 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 dispenseSnack(snack:String) { //分配零食
print("Dispensing (snack)")
}
func vend(itemNamed name:String) throws {
guard var 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 //存款减去费用
--item.count //存货减一
inventory[name] = item
dispenseSnack(name) //调函数说明正在分发物品
}
}
根据上面的描述可看出, 使用 定义属性 var vend = VendingMachine()
vend.coinsDeposited = 1
try vend.vend(itemNamed: "Chips")
Output: 错误处理.VendingMachineError.InsufficientFunds(9)
vend.coinsDeposited = 100
try vend.vend(itemNamed: "Chip")
Output: 错误处理.VendingMachineError.InvalidSelection
其他错误就不一一演示了~验证起来十分简单 2.2、用Do-Catch处理错误这里的过程相当于将上面的错误提示用一段闭包来处理 var vend = VendingMachine() vend.coinsDeposited = 100
do {
try vend.vend(itemNamed: "Chips")
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional (coinsNeeded) coins.")
}
Output: Dispensing Chips 这是无异常的输出结果,可以尝试修改成其他结果验证 2.3、将错误转换为可选值使用 如果抛出错误,那么这个表达式为 func someThrowingFunction() throws -> Int {
// ...
}
let x = try? someThrowingFunction()
let y: Int?
do {
y = try someThrowingFunction()
} catch {
y = nil
}
但是不管正不正常,x、y都是可选类型 let result = try?vend.vend(itemNamed: "XXX")
print(result) //结果是nil,因为货物不在清单中
Output: nil
2.4、使错误传递失效除 3、指定清理操作
这里的功能有点类似OC中异常处理机制中的 func testDefer() throws {
print("222221")
defer {
print("Defer1")
}
print("222222")
defer {
print("Defer2")
}
print("333333")
do {
defer {
print("Do-Catch Defer")
}
try vend.vend(itemNamed: "chippppp")
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let coinsNeeded) {
print("Insufficient funds. Please insert an additional (coinsNeeded) coins.")
}
}
try testDefer()
Output: 222221
222222
333333
Do-Catch Defer
Invalid Selection.
Defer2
Defer1
注意打印顺序,尤其 如果把do中的代码顺序换下 do {
try vend.vend(itemNamed: "chippppp")
defer {
print("Do-Catch Defer")
}
}
Output: 222221
222222
333333
Invalid Selection.
Defer2
Defer1
会发现 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |