3 ms·
More importantly is the ability to subclass exceptions, so it doesn't break client code. class MyBaseException(Exception): ... class MyExistingExc
by bb88 4y ago
More importantly is the ability to subclass exceptions, so it doesn't break client code.
class MyBaseException(Exception):
...
class MyExistingException(MyBaseException):
...
class MyBrandSpankingNewException(MyBaseException):
...
The client can catch the base exception no matter what new exceptions you create in the future.
- tialaramex 4y agoThis design claims MyBrandSpankingNewException was: * So different we just couldn't represent it as MyExistingException and yet * So similar that it's fine to just treat it as MyBaseException everywhere In my experience everywhere I see this pattern all we're doing with MyBaseException is giving it to a human, via logging, stderr, or whatever, which means this is a lot of complexity to just move some text. Although this strategy didn't formally "break client code" in practice if it matters the code will need to be rewritten to actually handle MyBrandSpankingNewException.
- d0mine 4y agoUsually a derived exception is a special case of the base exception e.g., OSError -> FileNotFoundError. Sometimes you want to handle specific error differently and sometimes you don't care. btw, it may be the reason to create a new subclass, to distinguish between cases when you do care.