swift语言点评十八-异常与错误
1、错误类型与枚举的结合
enum VendingMachineError: Error {
case invalidSelection
case 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 ...
随机推荐
- T7316 yyy的最大公约数(者)
题目背景 全场基本暴力 题目描述 输入输出格式 输入格式: 如图 输出格式: 如图 输入输出样例 输入样例#1: 如图 输出样例#1: 如图 说明 如图 这题用到了容斥原理和线性筛的一些东西, 表示没 ...
- javascript中封装获取样式属性值的兼容方法
function getStyle(obj, attr) { if (window.getComputedStyle) { return window.getComputedStyle(obj, nu ...
- 一个APP开发有那么难吗?
app开发 idea:产品设计喵有一个想法. 人员配置: 攻城狮:前端后端服务器齐撸 产品设计:设计原型/UI效果图(界面/交互)齐撸] 流程分析: 1.产品设计喵反复打磨自己的想法,明确要做什么样的 ...
- iOS系统结构
应用交互层.多媒体层.核心服务层.系统层. 参考官方文档apple Develop GuidesiOS Technologies IOS分为四级结构,由上到下为可触摸层,媒体层,核心服务层,核心系统层 ...
- javascript: 基于原型的面向对象编程
Douglas Crockford指出javascript是世界上最被误解的编程语言.由于javascript缺少常见的面向对象概念,许多程序猿认为javascript不是一个合适的语言.我在做第一个 ...
- ZBrush中绘制层是什么意思?
我们经常使用笔刷雕刻模型,在使用笔刷为头部模型添加一些纹理效果时,有时可能会有不满意的地方,但是很难修改,也很难把它还原为原来的状态,这时我们就可以使用Layers(绘制层)来将雕刻的部分分到每一个层 ...
- c/c++连接mysql数据库设置及乱码问题(vs2013连接mysql数据库,使用Mysql API操作数据库)
我的安装环境: (1)vs2013(32位版) (vs2013只有32位的 没有64位的,但是它可以编译出64位的程序) : (2)mysql-5.7.15(64位) vs2013中的设置(按步骤来 ...
- (3)pyspark----dataframe观察
1.读取: sparkDF = spark.read.csv(path) sparkDF = spark.read.text(path) 2.打印: sparkDF.show()[这是pandas中没 ...
- 清空chrome浏览器缓存
缓存是一个很深奥的东西,虽然查了半天,还是没有搞清楚,希望以后可以遇到前端大神,可以给一个傻瓜化的通俗易懂的解释 已经上线的,后续有迭代的软件,迭代的版本不应该手动清除缓存了,因为太麻烦,对客户来说不 ...
- com.jasson.im.api.APIClient jar包 下载
包名:ImApi2.3.jar 链接: https://pan.baidu.com/s/1SgeufcaH6y_K-AJEKDZDtw 提取码: 3v78 复制这段内容后打开百度网盘手机App,操作更 ...