PEP8 doesn't like broad exception handling like
try:
do_something()
except Exception:
handle_exception()
I know, there are good reasons for this like SystemExit
and KeyboardInterrupt
being handled in an un-inteded way or missing important error-cases.
But sometimes I think I have to handle "all other" exceptions like in this case:
while True:
try:
result = handle_request(get_request())
except (all, my, known, exceptions) as ex:
sophisticated_exception_handling()
except (KeyboardInterrupt, SystemExit):
raise
except Exception as ex:
# handle exotic situations which just didn't happen before
result = "something bad happened: %r" % ex
In this example I don't want to have my message loop interrupted just because some sub routine raises an exception I just didn't think about yet.
Of course I could just # pylint: disable
this line to get no warning - but that doesn't feel like the most sophisticated answer.
So my question is:
Is there some cool approach to nicely handle all exceptions and thus never fall out of my message handling loop without just ignoring a PEP8 warning?
Looks like you have a "special case, based on practicality, which should beat purity" mentioned in PEP20:
Special cases aren't special enough to break the rules.
Although practicality beats purity.
... and...
Errors should never pass silently.
Unless explicitly silenced.
I believe your code does not violate PEP20 (aka The Zen of Python)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With