I'm looking for a way to catch multiple types of errors in a catch
. I've tried fallthrough
and the comma separated style from a switch statement and neither works. The docs say nothing about catching multiple but pattern 1
. It's not clear to me which of the pattern syntaxes would work here.
Error definitions (sample):
enum AppErrors {
case NotFound(objectType: String, id: Int)
case AlreadyUsed
}
Works:
do {
//...
} catch AppErrors.NotFound {
makeNewOne()
} catch AppErrors.AlreadyUsed {
makeNewOne()
} catch {
print("Unhandled error: \(error)")
}
Does not compile, is it possible to do something like this?
do {
//...
} catch AppErrors.NotFound, AppErrors.AlreadyUsed {
makeNewOne()
} catch {
print("Unhandled error: \(error)")
}
catch let error as AppErrors where error == .NotFound
, I get the error messagebinary operator == cannot be applied to two AppErrors operands
. I think this is because these errorTypes have associated values. Is there a way around this? – Alan