Skip to content

Instantly share code, notes, and snippets.

@DivineDominion
Created March 12, 2025 14:36
Show Gist options
  • Save DivineDominion/8da46d9a3b63db3ababf666e3182de42 to your computer and use it in GitHub Desktop.
Save DivineDominion/8da46d9a3b63db3ababf666e3182de42 to your computer and use it in GitHub Desktop.
Demonstration how compiler cannot infer that typed errors are exhausted
enum MyError: Error {
case failureA
case failureB
}
func produceFailure() throws(MyError) -> Int {
throw .failureB
}
do throws(MyError) {
let result = try produceFailure()
print("Result:", result)
} catch MyError.failureA {
print("Failure A")
} catch MyError.failureB {
print("Failure B")
}
enum MyError: Error {
case failureA
case failureB
}
func produceFailure() throws(MyError) -> Int {
throw .failureB
}
func anotherFunction() {
do throws(MyError) {
let result = try produceFailure()
// `- error: errors thrown from here are not handled because the enclosing catch is not exhaustive
print("Result:", result)
} catch MyError.failureA {
print("Failure A")
} catch MyError.failureB {
print("Failure B")
}
}
anotherFunction()
enum MyError: Error {
case failureA
case failureB
}
func produceFailure() throws(MyError) -> Int {
throw .failureB
}
func anotherFunction() {
do throws(MyError) {
let result = try produceFailure()
print("Result:", result)
} catch MyError.failureA {
print("Failure A")
} catch MyError.failureB {
print("Failure B")
} catch {
// `- warning: case will never be executed
print("Failure:", error)
}
}
anotherFunction()
@DivineDominion
Copy link
Author

NB: The code doesn't work in a Playground. At all.
2025-03-12 15-37-07 Xcode - Playground playground@2x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment