On Thu, Sep 1, 2022, 9:12 PM Chris Angelico > > It depends on context whether it makes sense to define a custom > exception, and I agree that I frequently should define a custom exception. > In that case though, it would still be nice to have an appropriate generic > exception for that to inherit from, just as I would inherit from > `ValueError` for a special case of a value error. > > > Can you describe a situation where you'd want to catch > InvalidStateError from two unrelated libraries at the same time? > > TBH I'm actually a bit confused as to how you'd cope with > InvalidStateError anyway. In what situations would you be able to recover > from such an error (other than generic "log the error and continue", which > works fine with "except Exception")? >
I'm +0 on this. Maybe +0.5. Making a custom error isn't hard, but inheriting from something more in the ballpark is nicer. Doing `except Exception ` feels very likely to be too broad. If my code hits ZeroDivisionError, I probably want to do something different than I do for an ended subprocess or a closed file. Maybe they all are just "log and continue," sure. But for many resources, it's possible to require them, or at least something functionally equivalent/usable. Maybe *that* subprocess ended, but I might be able to launch another with suitable behavior. Maybe *that* file handle is closed, but I can reopen the same file. >
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/OX4L4SBH3GIHEL5XJ3GXGLCLMMMOCGR5/ Code of Conduct: http://python.org/psf/codeofconduct/