In swift, is it possible to use the shorter guard let try?
and get the occuring exception if entering the else block?
guard let smth = try? myThrowingFunc() else {
print(error) //can I access the exception here somehow?
return
}
vs
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error)
return
}
I have found page no 42 in "The Swift Programming Language (Swift 2.2 Prerelease)" where it states explicitly the following:
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.
So, this would rather be a feature request for apple then. As a matter of fact there's already some discussion on this topic here:
http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266