作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在更新我的代码以使用 Swift,我想知道如何打印与“全部捕获”子句匹配的异常的错误详细信息。我稍微修改了这个 Swift Language Guide Page 中的示例为了说明我的观点:
do {
try vend(itemNamed: "Candy Bar")
// Enjoy delicious snack
} catch VendingMachineError.InvalidSelection {
print("Invalid Selection.")
} catch VendingMachineError.OutOfStock {
print("Out of Stock.")
} catch VendingMachineError.InsufficientFunds(let amountRequired) {
print("Insufficient funds. Please insert an additional $\(amountRequired).")
} catch {
// HOW DO I PRINT OUT INFORMATION ABOUT THE ERROR HERE?
}
如果我捕获到意外异常,我需要能够记录导致异常的原因。
最佳答案
我刚刚弄明白了。我在 Swift 文档中注意到了这一行:
If a catch clause does not specify a pattern, the clause will match and bind any error to a local constant named error
所以,然后我尝试了这个:
do {
try vend(itemNamed: "Candy Bar")
...
} catch {
print("Error info: \(error)")
}
它给了我一个很好的描述。
关于ios - 如何在 Swift 中打印 'catch all' 异常的详细信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31352593/
我是一名优秀的程序员,十分优秀!