3 ms·
Why do you need to restrict to a single base type to exhaustively catch all listed?
by jayd16 1y ago
Why do you need to restrict to a single base type to exhaustively catch all listed?
- MBCook 1y agoFrom my reading you can’t say “throws(MyError, BadError, ThirdError)”. So you only have two options. You could use a non-checked throws like before, but we’re assuming you want it checked. Since you can only put one name between those parenthesis, the only way to do all three would be to have a base class and list that. Or at least that’s my understanding. However if you do that and there’s a fourth type called UnwantedError, that’s also valid to throw even though you don’t want to. It’s an interesting limit. I didn’t follow this feature, though I remember hearing about it, so I don’t know why that choose was made.
- 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.