Remove bare excepts
The biggest problem with bare exceptions is that they might absorb intended interruptions which should bubble up the call stack and therefore fail to stop motors or other critical things.
Common BaseExceptions
which are NOT Exception
:
-
KeyboardInterrupt
: process interrupt (CTRL-C orkill -2
) -
gevent.GreenletExit
: the current greenlet is requested to terminate (can be raised by any cooperative call) -
gevent.Timeout
: can be raised by any cooperative call. What caused it:- timeout context up in the call stack: the current call stack timed out
gevent.greenlet.Greenlet.get
gevent.event.Event.get
Many bare except:
and except: BaseExceptions
do not re-raise the exception. In case of KeyboardInterrupt
and gevent.GreenletExit
you should never absorb the exception, unless you are not in the main greenlet and you are sure the greenlet will exit after whatever cleanup you need to do. Even if you re-raise you need to be sure that the cleanup never hangs.
The biggest problem is gevent.Timeout
because you don't know whether it is caused by the section between try: ... except
or whether it comes from a timeout context up the stack. If it comes from up the stack, you should think about it like KeyboardInterrupt
or gevent.GreenletExit
. If not you should think about it like python's TimeoutError
which is NOT a BaseExceptions
.