0
0
Swiftprogramming~10 mins

Rethrowing functions in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a function that can rethrow errors from its closure parameter.

Swift
func perform(action: () throws -> Void) [1] {
    try action()
}
Drag options to blanks, or click blank then click option'
Arethrows
Bthrows
Ctry
Dthrowing
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throws' instead of 'rethrows' causes the function to always throw.
Using 'try' or 'throwing' is not valid in function declaration.
2fill in blank
medium

Complete the code to call a rethrowing function with a throwing closure.

Swift
[1] perform(action: {
    throw NSError(domain: "Error", code: 1)
})
Drag options to blanks, or click blank then click option'
Ado
Btry
Cthrow
Dcatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'do' or 'catch' inside the closure parameter is incorrect here.
Forgetting 'try' causes a compile error.
3fill in blank
hard

Fix the error in the function declaration to correctly rethrow errors from the closure.

Swift
func execute(task: () throws -> Void) [1] {
    try task()
}
Drag options to blanks, or click blank then click option'
Atry
Bthrows
Cthrow
Drethrows
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'throws' makes the function throw unconditionally.
Using 'throw' or 'try' in the declaration is invalid.
4fill in blank
hard

Fill both blanks to define a rethrowing function that accepts a closure and calls it.

Swift
func runOperation(operation: () [1] -> Void) [2] {
    try operation()
}
Drag options to blanks, or click blank then click option'
Athrows
Brethrows
Attempts:
3 left
💡 Hint
Common Mistakes
Marking the function as 'throws' instead of 'rethrows'.
Not marking the closure as 'throws'.
5fill in blank
hard

Fill all three blanks to create a rethrowing function that accepts a closure, calls it, and handles errors.

Swift
func handle(action: () [1] -> Void) [2] {
    do {
        try action()
    } catch {
        print([3])
    }
}
Drag options to blanks, or click blank then click option'
Athrows
Brethrows
Cerror
Derrors
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'errors' instead of 'error' in the catch block.
Marking the function as 'throws' instead of 'rethrows'.