是否可以使用 "try?" 在保护语句中检索异常?
Is it possible to retrieve an exception inside a guard-statement with "try?"?
在swift中,是否可以使用较短的guard let try?
和进入else块时得到出现的异常?
guard let smth = try? myThrowingFunc() else {
print(error) //can I access the exception here somehow?
return
}
对
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error)
return
}
我在 "The Swift Programming Language (Swift 2.2 Prerelease)" 中找到了第 42 页,其中明确说明了以下内容:
Another way to handle errors is to use try?
to convert the result to an optional. If the function throws an error, the specific error is discarded and the result is nil
. Otherwise, the result is an optional containing the value that the function returned.
所以,这更像是对苹果的功能请求。事实上,这里已经有一些关于这个话题的讨论:
http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266
在swift中,是否可以使用较短的guard let try?
和进入else块时得到出现的异常?
guard let smth = try? myThrowingFunc() else {
print(error) //can I access the exception here somehow?
return
}
对
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error)
return
}
我在 "The Swift Programming Language (Swift 2.2 Prerelease)" 中找到了第 42 页,其中明确说明了以下内容:
Another way to handle errors is to use
try?
to convert the result to an optional. If the function throws an error, the specific error is discarded and the result isnil
. Otherwise, the result is an optional containing the value that the function returned.
所以,这更像是对苹果的功能请求。事实上,这里已经有一些关于这个话题的讨论:
http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266