Python try/except: Showing the cause of the error after displaying my variables

Posted by NealWalters on Stack Overflow See other posts from Stack Overflow or by NealWalters
Published on 2010-12-30T05:43:26Z Indexed on 2010/12/30 5:54 UTC
Read the original article Hit count: 273

Filed under:
|

I'm not even sure what the right words are to search for. I want to display parts of the error object in an except block (similar to the err object in VBScript, which has Err.Number and Err.Description). For example, I want to show the values of my variables, then show the exact error. Clearly, I am causing a divided-by-zero error below, but how can I print that fact?

try: 
    x = 0 
    y = 1 
    z = y / x 
    z = z + 1 
    print "z=%d" % (z) 
except: 
    print "Values at Exception: x=%d y=%d " % (x,y) 
    print "The error was on line ..." 
    print "The reason for the error was ..." 

© Stack Overflow or respective owner

Related posts about python

Related posts about exception-handling