0
0
Swiftprogramming~20 mins

Error protocol conformance in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Error Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Swift error handling code?

Consider the following Swift code that defines an error and uses do-catch to handle it. What will be printed?

Swift
enum MyError: Error {
    case badInput
}

func testError() throws {
    throw MyError.badInput
}

do {
    try testError()
    print("No error")
} catch MyError.badInput {
    print("Caught badInput error")
} catch {
    print("Caught some other error")
}
ACaught badInput error
BCompilation error
CCaught some other error
DNo error
Attempts:
2 left
💡 Hint

Think about which error is thrown and which catch block matches it.

Predict Output
intermediate
2:00remaining
What error does this Swift code raise?

Given this Swift code, what error will be raised at runtime?

Swift
struct NotAnError {}

func throwSomething() throws {
    throw NotAnError()
}

try? throwSomething()
AType 'NotAnError' does not conform to protocol 'Error'
BNo error, code runs fine
CRuntime error: unexpected non-Error thrown
DCompilation error: missing throws keyword
Attempts:
2 left
💡 Hint

Check if the thrown type conforms to the Error protocol.

🔧 Debug
advanced
2:00remaining
Why does this Swift code fail to compile?

Examine the code below. Why does it fail to compile?

Swift
enum CustomError: Error {
    case errorOne
}

func mightThrow(_ flag: Bool) throws {
    if flag {
        throw "Error occurred"
    }
}

try? mightThrow(true)
AMissing throws keyword in function signature
BString does not conform to Error protocol
CCannot throw from non-throwing function
DEnum cases must have associated values
Attempts:
2 left
💡 Hint

Look at the type being thrown inside the function.

🧠 Conceptual
advanced
2:00remaining
Which Swift type correctly conforms to the Error protocol?

Which of the following Swift types correctly conforms to the Error protocol?

Aenum MyError {}
Bclass MyError {}
Cstruct MyError: CustomStringConvertible {}
Dstruct MyError: Error {}
Attempts:
2 left
💡 Hint

Remember that to be throwable, a type must explicitly conform to Error.

Predict Output
expert
3:00remaining
What is the output of this Swift error handling with pattern matching?

Analyze the following Swift code. What will it print?

Swift
enum NetworkError: Error {
    case timeout
    case serverError(code: Int)
}

func fetchData() throws {
    throw NetworkError.serverError(code: 500)
}

do {
    try fetchData()
} catch NetworkError.timeout {
    print("Timeout occurred")
} catch NetworkError.serverError(let code) where code >= 500 {
    print("Server error with code \(code)")
} catch {
    print("Other error")
}
ATimeout occurred
BOther error
CServer error with code 500
DCompilation error
Attempts:
2 left
💡 Hint

Check the thrown error and the pattern matching in the catch clauses.