Do-catch behavior in Xcode

While fixing a bug in my project, I catch an interesting behavior of do-catch:

enum KnownError: Error {
    case oops
enum UnknownError: Error {
    case unknown

func methodThatThrowUnknownError() throws {
    throw UnknownError.unknown

do {
    print("do start")
    try methodThatThrowUnknownError()
    print("do finish")
} catch let error as KnownError {

When we throw an UnknownError and our do-catch doesn't have a simple catch {...} in the end (that catch any error) - we will get stuck.
The print("exit") will never call.

I catch this behavior in Xcode 13.3, project with iOS 15.0.

Also, I tried to run this code via CLI and got:

I think, Xcode should show a live issue about missing common catch {...} block.
So, what do you think?

1 Like

This doesn't compile for me, I get the expected Errors thrown from here are not handled because the enclosing catch is not exhaustive error message.

XCode 13.4.1

1 Like

Like @GreatApe already said, your code shouldn't compile because you're not handling the "catch-all" case with just a simple catch. This is a requirement as of Swift 5.7 because the compiler can't possibly know what errors your methodThatThrowUnknownError() can potentially throw because Swift simply has no Precise Error Typing.