【发布时间】:2019-09-30 11:12:22
【问题描述】:
有没有办法根据错误枚举的关联值的值来捕获具有某种条件的错误?
例子:
enum Errors : Error {
case error1(str: String?) // optional associated value
case error2
case error3
}
func f() throws {
throw Errors.error1(str: nil)
}
do {
try f()
}
catch Errors.error1(let str) {
if(str != nil) {
print(str!)
}
else {
//throw the same error to be caught in the last catch
}
}
catch {
print("all other errors")
}
【问题讨论】:
标签: swift error-handling enums