Given that you let your LocksmithError
have some rawvalue type (e.g. Int
), you could, in a single catch
statement, bind the error thrown and use its rawValue
to test for inclusion into one of several error cases (using the where
clause after binding). E.g.:
enum FooError: Int, Error {
case err1 = 1, err2, err3, err4, err5
}
func foo(_ bar: Int) throws {
if let err = FooError(rawValue: bar) { throw err }
}
func tryFoo(_ bar: Int) {
do {
try foo(bar)
} catch let err as FooError where (1...4).contains(err.rawValue) {
print("Any of 1st through 4th error!")
} catch FooError.err5 {
print("5th error!")
} catch {}
}
tryFoo(1) // Any of 1st through 4th error!
tryFoo(4) // Any of 1st through 4th error!
tryFoo(5) // 5th error!
As suggested by @user28434 (thanks!), there's really no need to apply the rawValue
restraint as the same method above could be used to direcly see if the binded err
is a member of an array of given cases.
enum FooError: Error {
case err1, err2, err3
}
func foo(_ bar: Int) throws {
guard bar != 1 else { throw FooError.err1 }
guard bar != 2 else { throw FooError.err2 }
guard bar != 3 else { throw FooError.err3 }
}
func tryFoo(_ bar: Int) {
do {
try foo(bar)
} catch let err as FooError where [.err1, .err2].contains(err) {
print("1st or 2nd error!")
} catch FooError.err3 {
print("3rd error!")
} catch {}
}
tryFoo(1) // 1st or 2nd error!
tryFoo(2) // 1st or 2nd error!
tryFoo(3) // 3rd error!
This reduces to basically just a variation of the accepted answer (possibly useful for catch
blocks covering more than just two cases, but in such cases, possibly the error enum
should consider refactoring).