class ABC {
enum Error: ErrorTpe {
case a
case b
}
func a(a: String?) throws -> Int {
//抛出异常后方法会立即终止,defer是成功执行或者失败后都会执行的block
defer{
print("have a nice day")
}
guard let a = a else {
throw Error.a
}
}
}
let abc = ABC()
let string: String? = nil
//成功便执行 否者返回nil
try? abc.a(string)
//do catch
do{
try abc.a(string)
}catch let error as ABC.Error{
print(error)
}
网友评论