try, try! & try? what’s the difference, and when to use each?

In Swift 2.0, Apple introduced a new way to handle errors (do-try-catch). And few days ago in Beta 6 an even newer keyword was introduced (try?). Also, knew that I can use try!. What’s the difference between the 3 keywords, and when to use each?

Assume the following throwing function:

enum ThrowableError : ErrorType { case BadError }
func doSomething() throws -> String {
if everythingIsFine {
return "Everything is ok"
} else {
throw ThrowableError.BadError


You have 2 options when you try calling a function that may throw.

You can take responsibility of handling errors by surrounding your call within a do-catch block:

do {
    let result = try doSomething()
catch {
    // Here you know about the error
    // Feel free to handle to re-throw

Or just try calling the function, and pass the error along to the next caller in the call chain:

func doSomeOtherThing() throws -> Void {    
    // Not within a do-catch block.
    // Any errors will be re-thrown to callers.
    let result = try doSomething()


What happens when you try to access an implicitly unwrapped optional with a nil inside it? Yes, true, the app will CRASH! Same goes with try! it basically ignores the error chain, and declares a “do or die” situation. If the called function didn’t throw any errors, everything goes fine. But if it failed and threw an error, your application will simply crash.

let result = try! doSomething() // if an error was thrown, CRASH!


A new keyword that was introduced in Xcode 7 beta 6. It returns an optional that unwraps successful values, and catches error by returning nil.

if let result = try? doSomething() {
    // doSomething succeeded, and result is unwrapped.
} else {
    // Ouch, doSomething() threw an error.

Or we can use new awesome guard keyword:

guard let result = try? doSomething() else {
    // Ouch, doSomething() threw an error.
// doSomething succeeded, and result is unwrapped.

One final note here, by using try? note that you’re discarding the error that took place, as it’s translated to a nil. Use try? when you’re focusing more on successes and failure, not on why things failed.



One thought on “try, try! & try? what’s the difference, and when to use each?

  1. Pingback: Music is Everything. Songs to the TikTok Videos |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s