What if you could pass errors up the chain without writing extra error handling code everywhere?
Why Rethrowing functions in Swift? - Purpose & Use Cases
Imagine you have a function that calls another function which might throw an error. You want to pass the error up without handling it yourself. Doing this manually means writing extra code to catch and rethrow errors everywhere.
This manual approach is slow and error-prone because you must write repetitive try-catch blocks just to pass errors along. It clutters your code and makes it harder to read and maintain.
Rethrowing functions let you declare that your function can throw errors only if the function it calls throws. This way, you don't write extra error handling code, and errors flow naturally up the call chain.
func perform(action: () throws -> Void) throws {
do {
try action()
} catch {
throw error
}
}func perform(action: () throws -> Void) rethrows {
try action()
}It enables clean, concise error propagation without unnecessary boilerplate, making your code easier to write and understand.
When writing a sorting function that takes a comparison closure which might throw errors, rethrowing lets your sorting function pass those errors up without extra catch blocks.
Manual error passing requires repetitive try-catch blocks.
Rethrowing functions automatically propagate errors from called functions.
This leads to cleaner and more maintainable code.
Practice
rethrows keyword indicate in a Swift function?Solution
Step 1: Understand the role of
Therethrowsrethrowskeyword means the function itself does not throw errors unless the closure it accepts throws.Step 2: Compare options with this meaning
The function only throws an error if the closure it receives throws an error. correctly states this behavior. Options B, C, and D misunderstand the conditional throwing nature ofrethrows.Final Answer:
The function only throws an error if the closure it receives throws an error. -> Option CQuick Check:
rethrows means conditional throwing = A [OK]
- Thinking rethrows means always throws
- Confusing rethrows with throws
- Assuming rethrows catches errors
operation?Solution
Step 1: Identify correct placement of
The closure parameter must be declared withthrowsandrethrowsthrowsif it can throw, and the function must be markedrethrowsto propagate errors conditionally.Step 2: Check each option
func perform(operation: () throws -> Void) rethrows {} correctly declares the closure as() throws -> Voidand the function asrethrows. func perform(operation: () rethrows -> Void) throws {} incorrectly usesrethrowson the closure. func perform(operation: () throws -> Void) throws {} marks the function asthrowsinstead ofrethrows. func perform(operation: () -> Void) rethrows {} declares a non-throwing closure but marks the function asrethrows, which is invalid.Final Answer:
func perform(operation: () throws -> Void) rethrows {} -> Option DQuick Check:
Closure throws + function rethrows = A [OK]
- Using rethrows on closure instead of function
- Marking function as throws instead of rethrows
- Marking function rethrows with non-throwing closure
func execute(task: () throws -> String) rethrows -> String {
return try task()
}
func successTask() throws -> String {
return "Success"
}
func failTask() throws -> String {
throw NSError(domain: "Error", code: 1)
}
print(try execute(task: successTask))Solution
Step 1: Understand function behavior with successTask
Theexecutefunction callstask()which issuccessTaskhere. SincesuccessTaskreturns "Success" without throwing,executereturns "Success".Step 2: Analyze output of print statement
The print statement outputs the returned string "Success". No errors are thrown or caught here.Final Answer:
Success -> Option AQuick Check:
Successful closure call prints result = B [OK]
- Assuming error is thrown when closure succeeds
- Confusing rethrows with always throws
- Expecting silent error handling
func run(operation: () throws -> Void) rethrows {
do {
try operation()
} catch {
print("Caught error")
}
}
func risky() throws {
throw NSError(domain: "Fail", code: 0)
}
try run(operation: risky)Solution
Step 1: Understand rethrows and error catching
A function markedrethrowsmust not catch errors from the closure; it only propagates them. Catching errors inside breaks this rule.Step 2: Analyze the code's catch block
Therunfunction catches errors fromoperation()internally, so it cannot be markedrethrows. This causes a compile error.Final Answer:
The function cannot be marked rethrows if it catches errors internally. -> Option AQuick Check:
rethrows disallows internal catch = D [OK]
- Marking function rethrows but catching errors inside
- Confusing rethrows with throws
- Expecting rethrows to handle errors silently
processAll that takes an array of closures, each possibly throwing an error, and calls them all. If any closure throws, processAll should rethrow that error. Which is the best way to declare processAll?Solution
Step 1: Understand rethrows limitation
Therethrowskeyword only works if the function takes exactly one throwing closure parameter. Here,tasksis an array of closures, sorethrowscannot be used.Step 2: Choose correct error propagation
Since multiple closures can throw,processAllmust be markedthrowsto propagate errors. func processAll(tasks: [() throws -> Void]) throws { for task in tasks { try task() } } correctly usesthrows. func processAll(tasks: [() throws -> Void]) rethrows { for task in tasks { try task() } } incorrectly usesrethrows. func processAll(tasks: [() -> Void]) rethrows { for task in tasks { task() } } uses non-throwing closures. func processAll(tasks: [() throws -> Void]) { for task in tasks { try? task() } } ignores errors withtry?.Final Answer:
func processAll(tasks: [() throws -> Void]) throws { for task in tasks { try task() } } -> Option BQuick Check:
Multiple throwing closures require throws, not rethrows = C [OK]
- Using rethrows with multiple throwing closures
- Ignoring errors with try?
- Declaring non-throwing closures when errors can occur
Ready to prove it?
Pass the test and this pattern moves unknown
