【发布时间】:2016-05-08 20:33:21
【问题描述】:
我的程序有多个 do-try-catch 子句,但我始终使用相同的 catch 函数,我该如何将其分解?
例如
func tryCatch1 {
do{
try something.save
}catch let error as UserError{
print(error.description)
}
}
// Another try catch
func tryCatch2 {
do{
try somethingAgain.save
}catch let error as UserError{
print(error.description)
}
}
是否可以创建类似 "universal catch"
【问题讨论】:
-
为什么要捕获低级函数?简单地抛出异常。通常最好在最高级别捕获异常
-
@Paulw11 你能进一步解释一下吗?
-
以这种方式使用 try/catch 是没有意义的,不是吗?如果你不做一些有用的事情,你还不如使用 try!并且至少用户注意到出了问题。
-
@gnasher729 这只是一个例子来说明我的意思。
标签: ios swift error-handling try-catch