1、错误类型与枚举的结合

  1. enum VendingMachineError: Error {
  2. case invalidSelection
  3. case insufficientFunds(coinsNeeded: Int)
  4. case outOfStock
  5. }
  1. throw VendingMachineError.insufficientFunds(coinsNeeded: 5)

2、异常捕获与栈展开

Error handling in Swift resembles exception handling in other languages, with the use of the trycatch and throw keywords. Unlike exception handling in many languages—including Objective-C—error handling in Swift does not involve unwinding the call stack, a process that can be computationally expensive. As such, the performance characteristics of a throw statement are comparable to those of a return statement.

3、异常的传播:异常链

Only throwing functions can propagate errors. Any errors thrown inside a nonthrowing function must be handled inside the function.

func vend(itemNamed name: String) throws

nonthrowing function:异常传播的终结者。

局部处理与传播策略

The catch clauses don’t have to handle every possible error that the code in the do clause can throw. If none of the catch clauses handle the error, the error propagates to the surrounding scope.

传播的简化:

You use try? to handle an error by converting it to an optional value. If an error is thrown while evaluating the try? expression, the value of the expression is nil.

try!:切断错误传播链条,生产运行错误。

If an error actually is thrown, you’ll get a runtime error.

4、资源清理:defer{}

最新文章

  1. WebForm控件--2016年12月29日
  2. 关于MVC4.0中@Styles.Render用法与详解
  3. Lua学习笔记一
  4. HTML标签的嵌套
  5. JavaScript学习笔记-自定义滚动条
  6. MyEclipse删除不再使用的工作空间记录
  7. Win7下JDK环境变量的设置
  8. After install XAMPP
  9. hibernate-mapping的各种属性配置
  10. POJ 3522 Slim Span 最小差值生成树
  11. VMware 虚拟机安装 Mac OS X Mountain Lion 苹果系统
  12. [置顶] Hibernate的一个经典异常
  13. 扩展vbox硬盘大小
  14. clion idea jetbrain windows下搞c/c++
  15. Ubuntu常用软件安装(附带地址和卸载自带软件)
  16. F01:金融学第一定律:时间的价值
  17. linux系统磁盘空间满了怎么办看完这篇文章之后就知道怎么解决了
  18. 我的 FPGA 学习历程(14)—— PWM 脉冲宽度调制
  19. vue在body上面绑定enter事件
  20. bzoj 1531 Bank notes 多重背包/单调队列

热门文章

  1. JavaScript实现乘法表
  2. 洛谷P3357 最长k可重线段集问题(费用流)
  3. Android跳转到系统Wifi界面的方式
  4. ScrollView和ListView滑动冲突问题
  5. sql调优《二》
  6. depth peeling实现半透明
  7. Qwiklab'实验-API Gateway, AWS Lambda'
  8. HDU1231 最长连续子序列
  9. xgboost学习
  10. IOS - autoresizingMask