错误处理

2019-07-16  本文已影响0人  曹来东

错误类型

自定义错误

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}
enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}
var result = try divide(20, 11)

do-catch

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func test() {
    print(1111)
    do {
        print(2222)
        print(try divide(20, 0))
        print(33333)
    } catch let SomeError.illegalArg(msg) {
        print("参数异常",msg)
    } catch let SomeError.outOfBounds(size, index) {
        print("下标越界:","size = \(size)","index = \(index)")
    } catch let SomeError.outOfMemory {
        print("内存溢出")
    } catch {
        print("其他错误")
    }
    print(44444)
}
test()
//打印结果
1111
2222
参数异常 0不能作为除数
44444

处理Error的两种方式

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func test() throws {
    print(1111)
    print(try divide(20, 0))
    print(22222)
}
try test()
//打印结果
//1111
//Fatal error: Error raised at top level:

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func test() throws {
    print(1111)
    do {
        print(222)
        print(try divide(20, 0))
        print(3333)
    } catch let error as SomeError {
        print(error)
    }
    print(444)
}
try test()
//打印结果
1111
222
illegalArg("0不能作为除数")
444

try? try!调用可能会抛出Error的函数,这样就不用去处理Error

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func test() {
    print(111)
    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)
}
test()
//111
//2

a b是等价的

var a = try? divide(20, 10)
var b : Int?
do {
   b = try divide(20, 10)
} catch let error as SomeError {
    print(error)
}

rethrows

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func exec(_ fn: (Int , Int) throws ->  Int, _ num1: Int,_ num2: Int) rethrows {
    
    print(try fn(num1, num2))
}
//Fatal error: Error raised at top level:
try exec(divide(_:_:), 20, 0)

defer

enum SomeError: Error {
    case illegalArg(String)
    case outOfBounds(Int, Int)
    case outOfMemory
}

func divide(_ num1: Int, _ num2: Int)throws -> Int {
    if num2 == 0 {
        throw SomeError.illegalArg("0不能作为除数")
    }
    return num1 / num2
}

func open(_ fileName: String) -> Int {
    print("open")
    return 0
}
func close(_ file: Int) {
    print("close")
}
func processFile(_ filename: String) throws {
    let file = open(filename)
    defer {
        close(file)
    }
    //使用file
    //...
    try divide(20, 0)
    
    //close将会在这里调用
}
try processFile("test.txt")
//open
//close
//Fatal error: Error raised at top level:
func fn1() {
    print("fn1")
}
func fn2() {
    print("fn2")
}

func test() {
    defer {
        fn1()
    }
    defer {
        fn2()
    }
}
test()
//打印
fn2
fn1
上一篇下一篇

猜你喜欢

热点阅读