gpt4 book ai didi

ios - 如何在 Swift 中打印 'catch all' 异常的详细信息?

转载 作者:IT王子 更新时间:2023-10-29 04:57:22 27 4
gpt4 key购买 nike

我正在更新我的代码以使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com