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.
Another way to catch all Python exceptions when it occurs during runtime is to use the raise keyword. It is a manual process wherein you can optionally pass values to the exception to clarify the reason why it was raised. if x <= 0: raise ValueError(“It is not a positive number!”)
It is an event which is thrown at runtime. It protects the code and run the program even after throwing an exception. Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception.
Since Exception is the base class of all exceptions, it will catch any exception.
The try and except block in Python is used to catch and handle exceptions. Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program's response to any exceptions in the preceding try clause.
except Exception:
vs except BaseException:
:
The difference between catching Exception
and BaseException
is that according to the exception hierarchy exception like SystemExit, KeyboardInterrupt and GeneratorExit will not be caught when using except Exception
because they inherit directly from BaseException
.
except:
vs except BaseException:
:
The difference between this two is mainly in python 2 (AFAIK), and it's only when using an old style class as an Exception to be raised, in this case only expression-less except clause will be able to catch the exception eg.
class NewStyleException(Exception): pass try: raise NewStyleException except BaseException: print "Caught" class OldStyleException: pass try: raise OldStyleException except BaseException: print "BaseException caught when raising OldStyleException" except: print "Caught"
If you need to catch all exceptions and do the same stuff for all, I'll suggest you this :
try: #stuff except: # do some stuff
If you don't want to mask "special" python exceptions, use the Exception base class
try: #stuff except Exception: # do some stuff
for some exceptions related management, catch them explicitly :
try: #stuff except FirstExceptionBaseClassYouWantToCatch as exc: # do some stuff except SecondExceptionBaseClassYouWantToCatch as exc: # do some other stuff based except (ThirdExceptionBaseClassYouWantToCatch, FourthExceptionBaseClassYouWantToCatch) as exc: # do some other stuff based
The exception hierarchy from the python docs should be a usefull reading.
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