0
0
Swiftprogramming~20 mins

Rethrowing functions in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Rethrowing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a rethrowing function with no error
What is the output of this Swift code when the closure does not throw an error?
Swift
func perform(action: () throws -> Void) rethrows {
    try action()
    print("Action completed")
}

try? perform {
    print("Hello")
}
AHello\nAction completed
BAction completed
CHello
DRuntime error
Attempts:
2 left
💡 Hint
Remember that the closure prints first, then the function prints after successful execution.
Predict Output
intermediate
2:00remaining
Error propagation in rethrowing function
What happens when the closure passed to this rethrowing function throws an error?
Swift
enum SampleError: Error {
    case failed
}

func execute(task: () throws -> Void) rethrows {
    try task()
    print("Task done")
}

try? execute {
    throw SampleError.failed
}
ATask done
BTask done\nError thrown
CRuntime crash
DNo output, error is caught silently
Attempts:
2 left
💡 Hint
The try? converts error to nil and suppresses output after error.
🔧 Debug
advanced
2:00remaining
Identify the error in rethrowing function usage
Which option causes a compile-time error in this Swift code?
Swift
func riskyOperation(action: () throws -> Void) rethrows {
    try action()
}

func callRisky() {
    try riskyOperation {
        print("Running")
    }
}
AThe code compiles and runs without error
BError: 'riskyOperation' cannot be marked 'rethrows' here
CError: 'callRisky' must be marked 'throws' because it calls a throwing function
DError: Missing 'try' keyword before 'riskyOperation' call
Attempts:
2 left
💡 Hint
Check if the calling function handles errors properly.
🧠 Conceptual
advanced
2:00remaining
Understanding rethrowing function constraints
Which statement about rethrowing functions in Swift is TRUE?
AA rethrowing function can throw errors even if the closure does not throw
BA rethrowing function only throws if the closure passed to it throws
CA rethrowing function must always be marked with 'throws' keyword
DA rethrowing function cannot call non-throwing closures
Attempts:
2 left
💡 Hint
Think about when the function propagates errors.
Predict Output
expert
2:00remaining
Output of nested rethrowing functions with error
What is the output of this Swift code snippet?
Swift
enum MyError: Error {
    case fail
}

func outer(action: () throws -> Void) rethrows {
    print("Outer start")
    try inner(action: action)
    print("Outer end")
}

func inner(action: () throws -> Void) rethrows {
    print("Inner start")
    try action()
    print("Inner end")
}

try? outer {
    print("Action running")
    throw MyError.fail
}
AOuter start\nInner start\nAction running
BOuter start\nInner start\nAction running\nInner end\nOuter end
COuter start\nInner start\nAction running\nOuter end
DOuter start\nInner start
Attempts:
2 left
💡 Hint
Trace the prints until the error is thrown and stops execution.