NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How to use the rethrows keyword

Swift version: 5.2

Paul Hudson    @twostraws   

The rethrows keyword is used when you write a function (let’s call it A) that accepts a throwing function as a parameter (let’s call it B). If function B throws errors, then the function A becomes a throwing function too, but if function B doesn’t throw errors then neither does function A.

First, here’s a simple function that accepts a username and always throws an error because biometric authentication isn’t available:

extension String: Error { }

func authenticateBiometrically(_ user: String) throws -> Bool {
    throw "Failed"
}

That little String extension allows us to throw strings as errors, which saves a little time.

Now here’s a second function that doesn’t throw:

func authenticateByPassword(_ user: String) -> Bool {
    return true
}

So, biometric authentication (Touch ID, Face ID) always throws an error, and password authentication always works.

Now we want to write an authentication function that can either run biometric authentication or password authentication depending on what its given. Because one of the two possibilities can throw, we mark its parameter as throwing, like this: method: (String) throws -> Bool.

What we’re saying is that this function might be able to throw, not that it must throw.

Try adding this function now:

func authenticateUser(method: (String) throws -> Bool) throws {
    try method("twostraws")
    print("Success!")
}

We can now call that function like this:

do {
    try authenticateUser(method: authenticateByPassword)
} catch {
    print("D'oh!")
}

Now for the important part: we both know that authenticateByPassword() doesn’t throw errors, and Swift can see that too, so if we change the definition of authenticateUser from throws to rethrows Swift will no longer require us to use do/catch when passing it a non-throwing parameter.

Change the function to this:

func authenticateUser(method: (String) throws -> Bool) rethrows {
    try method("twostraws")
    print("Success!")
}

Now Xcode will give you a warning: the catch block later on is unreachable because authenticateUser will never throw errors. But if you were to call it using authenticateBiometrically then you would need the do/catch blocks – Swift is able to evaluate the flow of our code much better, which means we need to write less code.

Hacking with Swift is sponsored by Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0 – learn more in my book Pro Swift

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.1/5

Link copied to your pasteboard.