3 ms·
> the only way to do all three would be to have a base class and list that I think the Swift way would be enum FooError: Error { case myError(MyEr
by Someone 1y ago
> the only way to do all three would be to have a base class and list that
I think the Swift way would be
enum FooError: Error {
case myError(MyError)
case badError(BadError)
case thirdError(ThirdError)
}
and declare your function as throws(FooError). That forces handlers of those exceptions to consider all cases.
Edit: I think there’s a case for having the compiler synthesize that if one writes something like throws(MyError||BadError||ThirdError), but I’m not sure I’d would like to have such magic. Would two functions with such a declaration throw the same type of exception? That might matter if you do stats on exceptions logged, for example)
- MBCook 1y agoIt does work that way, but I was thinking of the case where FooError was one of the three types you wanted to throw. You can also declare errors as structs, so that’s where you could use inheritance.