SE192 Non Exhaustive Enums and Errors

With the acceptance of SE192 Non Exhaustive Enums, what happens with Error enums? EG:

public enum SomeAppleError: Error {
    case anError
}
...
        do {
            result = try someFuncThatThrows()
        } catch SomeAppleError.anError {
            return result
        } catch {
            defaultAction(error)
        }

Is the plan to allow?

        do {
            result = try someFuncThatThrows()
        } catch SomeAppleError.anError {
            return result
        } catch {
            defaultAction(error)
        } unknown catch {
            unkownAction(error)
        }

IE is the plan to allow the catching of unknown errors like other unknown enums from Apple frameworks?

1 Like

Swift does not support typed throws, which means any function that throws (or rethrows) can potentially (as far as the compiler is concerned) throw any type of error—including types not known at compile-time, such as errors provided by 3rd-party client code.

Consequently, the only way to make an exhaustive catch is with a catch-all clause. There is no sense of “handling all known cases” in a catch the way there is in a switch, because there is no limit to the type of error that may be thrown.

However, you are perfectly free to utilize exhaustive switching within a catch block, for example:

do {
  try foo()
} catch let error as SomeError {
  switch error {
  case .knownCase: doSomething()
  unknown case _: doSomethingElse()
  }
} catch {
  whateverNeedsToBeDone()
}
2 Likes

Good point!