There’s something I did not think about when playing around with exceptions.
Suppose we have the following 3 exceptions
class Size {};
class TooBig : public Size {};
class TooSmall : public Size {};
Now, if we throw a TooSmall
exception and our try/catch
block looks like this
try {
dangerousFunction();
}
catch(TooBig) {
cout << "too big";
}
catch(TooSmall) {
cout << "too small";
}
catch(Size) {
cout << "Error in size";
}
Now, I may need to review some inheritance, but this was my thinking.
TooSmall
calls the Size
constructor, and we now that TooBig
derives from Size
.
So why exactly do we get “too small” and not “too big”? Isn’t TooBig
technically still a size
and we called size
constructor? Any explanation will be appreciated.
1