Swift 2 运行 代码如果没有异常
Swift 2 run code if no exceptions
我目前正在编写一个可能会引发错误的 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' 不是 的副本,因为 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
import Foundation
enum Error:ErrorType {
case Error1
case Error2
}
func foo() throws -> Void {
switch arc4random_uniform(3) {
case 0: throw Error.Error1
case 1: throw Error.Error2
default:
return
}
}
for i in 0...6 {
do {
try foo()
print("no error code")
// you can put here as much
// code, as you need
let i = random() % 100
let j = random() % 100
let k = i + j
print("\(i) + \(j) = \(k)")
} catch Error.Error1 {
print("error 1 code")
} catch Error.Error2 {
print("error 2 code")
}
}
结果
no error code
83 + 86 = 169
error 1 code
error 2 code
error 1 code
no error code
77 + 15 = 92
error 2 code
no error code
93 + 35 = 128
注意,不要混淆错误和异常。这些是完全不同的概念
在你的伪代码中
Run and Ceck a function that could throw for All errors
If the function does not throw an error
Do this code
...
If error1
Do this code
If error2
Do this code
我目前正在编写一个可能会引发错误的 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' 不是
我想做的是:
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
import Foundation
enum Error:ErrorType {
case Error1
case Error2
}
func foo() throws -> Void {
switch arc4random_uniform(3) {
case 0: throw Error.Error1
case 1: throw Error.Error2
default:
return
}
}
for i in 0...6 {
do {
try foo()
print("no error code")
// you can put here as much
// code, as you need
let i = random() % 100
let j = random() % 100
let k = i + j
print("\(i) + \(j) = \(k)")
} catch Error.Error1 {
print("error 1 code")
} catch Error.Error2 {
print("error 2 code")
}
}
结果
no error code
83 + 86 = 169
error 1 code
error 2 code
error 1 code
no error code
77 + 15 = 92
error 2 code
no error code
93 + 35 = 128
注意,不要混淆错误和异常。这些是完全不同的概念
在你的伪代码中
Run and Ceck a function that could throw for All errors
If the function does not throw an error
Do this code
...
If error1
Do this code
If error2
Do this code