swift语言点评十八-异常与错误
1、错误类型与枚举的结合
enum VendingMachineError: Error {case invalidSelectioncase insufficientFunds(coinsNeeded: Int)case outOfStock}
throw VendingMachineError.insufficientFunds(coinsNeeded: 5)
2、异常捕获与栈展开
Error handling in Swift resembles exception handling in other languages, with the use of the try, catch 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{}
swift语言点评十八-异常与错误的更多相关文章
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十四-继承
Overriding A subclass can provide its own custom implementation of an instance method, type method, ...
- swift语言点评十-Value and Reference Types
结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...
- swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...
- swift语言点评十五-$0
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...
- swift语言点评十二-Subscripts
Classes, structures, and enumerations can define subscripts, which are shortcuts for accessing the m ...
- Swift语言指南(十)--字符串与字符
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...
- IOS系列swift语言之课时八
这节课需要讲的就是可选链,内存管理,引用计数,unowned解决 //: Playground - noun: a place where people can play import UIKit / ...
- swift语言点评八-枚举
总结:swift中的枚举可以看作变量可以作为case匹配参数的类 Enumerations 枚举的作用:状态列举与匹配 枚举值与类型 If a value (known as a “raw” valu ...
随机推荐
- function——函数声明头的提升和预解析
函数: 即function语句的集合,就是将多个语句封装到一起: 函数的执行要会自己遍历,遇见函数 a():执行语句,就要移交控制权,函数执行完毕之后,控制权又移交回来了! 函数的参数要罗列在func ...
- 更博不能忘——webpack学习笔记
webpack是前端构建系统就像grunt和gulp(之前有学习过grunt所以也就大概明白webpack是个什么东西),所以说前端技术大部分还真是相通的,现在觉得当时多看的东西并不白费,虽然长时间不 ...
- ZBrush中如何做不同图案的遮罩
ZBrush®软件中不仅可以创建矩形遮罩还可以创建有图案的遮罩,且是非常简单有效的,那么究竟怎样做出神奇的效果,本文将为您详细讲解. 有关反转遮罩.清除遮罩的详细内容,请点击:ZBrush中如何反选遮 ...
- 按时间划分备份MySQL脚本
按时间划分备份MySQL脚本 #!/bin/bash BASE_PATH=/data/dump/ JIRA_FILE_NAME=ZY798-`date +%Y%m%d%H%M%S`; cd /usr/ ...
- HDU 1028 Ignatius and the Princess III(母函数整数拆分)
链接:传送门 题意:一个数n有多少种拆分方法 思路:典型母函数在整数拆分上的应用 /********************************************************** ...
- VUE:事件处理和表单输入绑定
事件处理 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- LeetCode 11. Container With Most Water 单调队列
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- js本地对象——Date()
Date()是JavaScript的本地对象,用于获取当前的时间,包括年.月.日.时.分.秒,可以精确到毫秒级:该对象返回的是UTC 协调世界时(Coordinated Universal Time) ...
- Mysql 5.7 官方文档翻译
始于 2017年4月1日-愚人节 1.1 MySQL 5.7 新功能 本章节介绍了MySQL 5.7 新版本中新增.废弃.删除的功能. 在1.5章节 Section 1.5, "Server ...
- PatentTips - Sleep state mechanism for virtual multithreading
BACKGROUND The present disclosure relates generally to information processing systems and, more spec ...