5 ms·
I’m not familiar with Rust, but Swift has three options and it sounds like it may be similar. In Swift code that throws when called must have “try” in front of
by MBCook 2y ago
I’m not familiar with Rust, but Swift has three options and it sounds like it may be similar.
In Swift code that throws when called must have “try” in front of it, making it really obvious where that’s going on. Your three options:
try - calls the code and either returns the like normal or an error that you’re forced to handle in a catch.
try? - calls the code and returns the value or nil (null value) if an error is thrown.
try! - calls the code and returns the value. If the function throws an error your app panics.
It’s quite nice. You can choose to handle the error when you need to. If you don’t really care about the specifics of the error and just want to treat any kind as a failure try? cleans up your code.
And try! lets you avoid writing boilerplate when you know it’s impossible for the error to be thrown but the compiler can’t deduce that from the source alone.