Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing the full exception

I don't know what it's called, but when I'm in the python live interpreter, I get a trail of statements in the form 'File , line ' etc, showing all the places where something went wrong so I can pinpoint the origin. Is this information an attribute of the exception that I can access? I'm looking for some solution like:

try:
     do_something_wrong()
except Exception as e: 
     print e.really_useful_information

What should I use as 'really_useful_information'? If it isn't possible, how else could I access this information while continuing to run the program?

like image 780
hedgehogrider Avatar asked Dec 04 '25 12:12

hedgehogrider


2 Answers

You could use traceback.print_exc():

import traceback
try:
    ...
except Exception as e:
    traceback.print_exc()
like image 69
unutbu Avatar answered Dec 06 '25 00:12

unutbu


What you are looking for is called a stack trace, if I understand you correctly. Take a look at the traceback module: http://docs.python.org/2/library/traceback.html Specifically traceback.print_exception() and traceback.print_exc().

like image 27
EClaesson Avatar answered Dec 06 '25 01:12

EClaesson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!