Remove bare except's and document broad exceptions.
See original GitHub issueI’m speculating that Travis changed something because the build which is failing is just a trivial blacklist edit.
https://chat.stackexchange.com/transcript/message/40709387#40709387
flake8
chokes on bare except:
clauses and I certainly agree that we should use a lot fewer of these – ideally none at all.
(The rest is a rant. You might want to stop reading here.)
If we have to have them, there should be a maximum of one per call tree, and as far up the call chain as possible. For example,
try:
something
try:
something else
except:
pass
except:
pass
… should probably be refactored to not use a bare except:
in the inner scope, and possibly should not use try
at all there as the exception will be captured by the caller anyway, and this sort of structure is extremely hard to understand and debug (and the real-life cases will be much more complex than the above).
As a rule of thumb, I like to put the ugliest code in main
and keep the internal functions as clean and obvious as possible, and even have them raise
errors when they need to (trusting that the ugliness of coping with the exception needs to be specific to each caller anyway, but we can keep things simple and elegant inside library functions).
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
So then, since it’s raised, I’ll leave this comment here.
As of revision ee2a44d4afcf9e31e96131608706ab6e4e8b631b, these broad exceptions exist.
Every single non-capturing broad exception explained:
chatcommands.py
: The only statement inside thetry
block is amsg.delete()
call. One needs to be familiar with ChatExchange to determine what could actually be raised here, but I’m not.chatcommunicate.py
: It’s appropriate here, to catch all exceptions raised from chat command functions (exceptCmdException
which will have its message returned).excepthook.py
: Should be obvious that it’s appropriate here.fix_pickles.py
: I remember that it’s already replaced withOSError
in the up-to-datemaster
branch …?metasmoke.py
: Not sure what could be raised, left as-isnocrash.py
: It’s followed by aexcept BaseException
soKeyboardInterrupt
s andSystemExit
s are correctly caught. ~Could be~ Already done in a more elegant way.spamhandling.py
: Looks like it’s intended (I left a wrong comment) so that any exception popped up inhandle_spam()
is handled by the exception hook.There are another 12
except Exception as e
’s that all seems to be correctly handled (logged and/or force reboot) and don’t belong to this topic.To sum up, what needs to be replaced by more specific exceptions are the 3 in
chatcommands.py
and the one inmetasmoke.py
. This time it really should be the last of them.I would argue that this issue isn’t fully finished until all the
except Exception
s are replaced with their specific Exceptions.