I want to catch a Python exception and print it rather than re-raising it. For example:
def f(x):
try:
return 1/x
except:
print <exception_that_was_raised>
This should then do:
>>> f(0)
'ZeroDivisionError'
without an exception being raised.
Is there a way to do this, other than listing each possible exception in a giant try-except-except…except clause?
use the
messageattribute of exception ore.__class__.__name__if you want the name of the Base exception class , i.eZeroDivisionError'in your caseIn python 3.x the
messageattribute has been removed so you can simply useprint(e)ore.args[0]there, ande.__class__.__name__remains same.