【发布时间】:2015-11-23 16:51:14
【问题描述】:
我目前正在编写一个可能引发错误的 Swift 2 应用程序。为了防止它崩溃我的代码,我使用do-try-catch。
在 python 中我们可以这样做:
try:
functionThatThrowsError()
except exceptionOne:
pass # exceptionOne was caught
except exceptionTwo:
pass # exceptionTwo was caught
else:
pass # No Errors in code
我目前在 Swift 中有这段代码:
do
{
try functionThatThrowsError
} catch exceptionOne {
//Do stuff
} catch exceptionTwo {
//Do Stuff
}
我将如何在 Swift 2 中做到这一点?
编辑: 'posible duplicate' 不是 Swift do-try-catch syntax 的重复项,因为 do-try-except 不检查代码是否成功执行。
我想做的是这样的:
Run a function that could throw an error
Check for one type of error
Do code if the error happened
Check for another error type that occurred
Do code if that error happened
If the function does not throw an error
Do this code
【问题讨论】: