PDA

View Full Version : Is it possible to get Python exception errors



-grubby
June 22nd, 2008, 10:04 PM
Is it possible to have an except statement print out debug info from python (IE: the traceback info). I want to recieve debug info, but not have my program crash. Can I do this? And if so, how?

days_of_ruin
June 22nd, 2008, 10:19 PM
edit:oops!

days_of_ruin
June 22nd, 2008, 10:20 PM
Have not tested this but:

import sys

try:
#do something
except:
print sys.stderr

Can+~
June 22nd, 2008, 10:20 PM
Easiest way to do it:


try:
x = 1/0
except ZeroDivisionError, err:
print "Someone set up us the bomb!",err

http://docs.python.org/tut/node10.html

days_of_ruin
June 22nd, 2008, 10:33 PM
Easiest way to do it:


try:
x = 1/0
except Exception, err:
print "Someone set up us the bomb!",err

http://docs.python.org/tut/node10.html
This will work for any error

-grubby
June 22nd, 2008, 10:50 PM
OK, I've tried all your methods, none of them are the one I wanted. To be more specific, I want it so if an exception occurs, it outputs the traceback info (as if the program crashed) but keeps running.

Wybiral
June 22nd, 2008, 11:19 PM
Actually, true to the Python cliche of having every module you need, you can type "import traceback" and use the traceback (http://docs.python.org/lib/module-traceback.html) module :) (after catching the exception)

Examples here (http://docs.python.org/lib/traceback-example.html)

(this is a powerful tool to have when you're running a server or something and you need to remotely log the exceptions and the stack traceback)