In Python, what’s the best way to catch “all” exceptions?
except: # do stuff with sys.exc_info()[1]
except BaseException as exc:
except Exception as exc:
The catch may be executing in a thread.
My aim is to log any exception that might be thrown by normal code without masking any special Python exceptions, such as those that indicate process termination etc.
Getting a handle to the exception (such as by the clauses above that contain exc) is also desirable.
If you need to catch all exceptions and do the same stuff for all, I’ll suggest you this :
If you don’t want to mask “special” python exceptions, use the Exception base class
for some exceptions related management, catch them explicitly :
The exception hierarchy from the python docs should be a usefull reading.