#73: try?, try! and do/catch in Swift

Swift has three common styles for handling throwing calls.

let value = try? loadConfig()
let surelyValue = try! loadBundledConfig()

Use try? when failure should just turn into nil. Use try! only when failure is impossible or should crash loudly during development.

For real error handling, use do/catch:

do {
    let config = try loadConfig()
    print(config)
} catch {
    print(error)
}

I'm also on Twitter and GitHub.

Subscribe via RSS.