错误类型
1、开发过程常见的错误
语法错误(编译报错)
逻辑错误
运行时错误(可能会导致闪退,一般也叫做异常),也是我们今天讲的重点
自定义错误
1、Swift中可以通过Error协议自定义运行时的错误信息
enum SomeError: Error {
case illegalArg(String)
case outOfBounds(Int, Int)
case outOfMemory
}
2、函数内部通过throw抛出自定义Error,可能会抛出Error的函数必须加上throws声明
func divide(_ num1: Int, _ num2: Int) throws -> Int {
if num2 == 0 {
throw SomeError.illegalArg("0不能作为除数")
}
return num1 / num2
}
3、需要使用try调用可能会抛出Error的函数
4、可以使用do-catch捕捉Error
func test() {
do {
try divide(20, 0)
} catch let SomeError.illegalArg(msg) {
print("参数异常:", msg)
} catch let SomeError.outOfBounds(size, index) {
print("下标越界:", "size = \(size), index = \(index)")
} catch SomeError.outOfMemory {
print("内存溢出")
} catch {
print("其他错误")
}
}
5、抛出Error后,try下一句直到作用域结束的代码都停止运行
处理Error
1、处理Error的2种方式
a、通过do-catch捕捉Error
b、不捕捉Error,在当前函数增加throws声明,Error将自动抛给上层函数
func test() throws {
print(try divide(200, 0))
}
如果最顶层函数(main函数)依然没有捕捉Error,那么程序将终止
以下是几种error的处理方法:
func test() throws {
do {
print(try divide(200, 0))
} catch let error as SomeError {
print(error)
}
}
func test() throws {
do {
print(try divide(200, 0))
} catch is SomeError {
print("SomeError")
}
}
do {
try divide(20, 0)
} catch let error {
switch error {
case let SomeError.illegalArg(msg):
print("参数异常:", msg)
default:
print("其他异常")
}
}
我们通过一个例子来总结一下处理Error的2种方式:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
try test0()
}
func test0() throws -> Void {
try test1()
}
func test1() throws -> Void {
try test2()
}
func test2() throws -> Void {
do {
print(try divide(200, 0))
} catch is SomeError {
print("This is SomeError")
}
}
try?、try!
1、可以使用try?、try!调用可能会抛出Error的函数,这样就不用去处理Error
func test() -> Void {
print("1")
var result1 = try? divide(20, 10) // Optional(2), Int?
var result2 = try? divide(20, 0) // nil
var result3 = try! divide(20, 10) // 2, Int
print("2")
}
2、a、b是等价的
var a = try? divide(20, 0)
var b: Int?
do {
b = try divide(20, 0)
} catch {
b = nil
}
rethrows
1、rethrows表明:函数本身不会抛出错误,但调用闭包参数抛出错误,那么它会将错误向上抛
func exec(_ fn: (Int, Int) throws -> Int, _ num1: Int, _ num2: Int) rethrows -> Void {
print(try fn(num1, num2))
}
defer
1、defer语句:用来定义以任何方式(抛错误、return等)离开代码块前必须要执行的代码
defer语句将延迟到当前作用域结束之前执行
func open(_ filename: String) -> Int {
print("open")
return 1
}
func close(_ file: Int) -> Void {
print("close")
}
func processFile(_ filename: String) throws -> Void {
let file = open(filename)
defer {
close(file)
}
try divide(20, 0)
}
2、defer语句的执行顺序与定义顺序相反
func fn1() -> Void {
print("fn1")
}
func fn2() -> Void {
print("fn2")
}
func testDefer() -> Void {
defer {
fn1()
}
defer {
fn2()
}
} //fn2 fn1
网友评论