swift2 异常处理

enum Error : ErrorType {
    case DivisionError
}
func divide(a: Int, b: Int) throws -> Int {
    if b == 0 {
        //使用throws来指定方法来抛出一个错误
	throw Error.DivisionError
    }
    return a/b
}

do {
    let result = try 
    //运行相应的代码
    divide(a: Int, b: Int)
} catch let Error. DivisionError {
    print("error")
}

转载收藏地址
http://appventure.me/2015/06/19/swift-try-catch-asynchronous-closures/

发表评论

电子邮件地址不会被公开。 必填项已用*标注