Updated for Xcode 15
When we call a function that might throw errors, we either call it using try
and handle errors appropriately, or if we’re certain the function will not fail we use try!
and accept that if we were wrong our code will crash. (Spoiler: you should use try!
very rarely.)
However, there is an alternative: if all we care about is whether the function succeeded or failed, we can use an optional try to have the function return an optional value. If the function ran without throwing any errors then the optional will contain the return value, but if any error was thrown the function will return nil. This means we don’t get to know exactly what error was thrown, but often that’s fine – we might just care if the function worked or not.
Here’s how it looks:
enum UserError: Error {
case badID, networkFailed
}
func getUser(id: Int) throws -> String {
throw UserError.networkFailed
}
if let user = try? getUser(id: 23) {
print("User: \(user)")
}
The getUser()
function will always throw a networkFailed
error, which is fine for our testing purposes, but we don’t actually care what error was thrown – all we care about is whether the call sent back a user or not.
This is where try?
helps: it makes getUser()
return an optional string, which will be nil if any errors are thrown. If you want to know exactly what error happened then this approach won’t be useful, but a lot of the time we just don’t care.
If you want, you can combine try?
with nil coalescing, which means “attempt to get the return value from this function, but if it fails use this default value instead.”
Be careful, though: you need to add some parentheses before nil coalescing so that Swift understands exactly what you mean. For example, you’d write this:
let user = (try? getUser(id: 23)) ?? "Anonymous"
print(user)
You’ll find try?
is mainly used in three places:
guard let
to exit the current function if the try?
call returns nil
.BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.