48

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 ..." 
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
NealWalters
  • 17,197
  • 42
  • 141
  • 251

7 Answers7

106
try:  
    1 / 0 
except Exception as e: 
    print(e)
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
30

If you're expecting a DivideByZero error, you can catch that particular error

import traceback
try:
  x = 5
  y = 0
  print x/y
except ZeroDivisionError:
  print "Error Dividing %d/%d" % (x,y)
  traceback.print_exc()
except:
  print "A non-ZeroDivisionError occurred"

You can manually get the line number and other information by calling traceback.print_exc()

sahhhm
  • 5,325
  • 2
  • 27
  • 22
  • Thanks. I was really looking for the "traceback" - couldn't remember what it was called, because I was looking for an error or exception object. – NealWalters Dec 30 '10 at 06:04
  • The DivideByZero was just a simple error used for my post, my actual situation is much more complex. – NealWalters Dec 30 '10 at 06:10
13

The string value of the exception object will give you the reason. The traceback module will allow you access to the full traceback.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
7

A better approach is to make use of the standard Python Logging module.

import sys, traceback, logging

logging.basicConfig(level=logging.ERROR)

try: 
    x = 0 
    y = 1 
    z = y / x 
    z = z + 1 
    print "z=%d" % (z) 
except: 
    logging.exception("Values at Exception: x=%d y=%d " % (x,y))

This produces the following output:

ERROR:root:Values at Exception: x=0 y=1 
Traceback (most recent call last):
  File "py_exceptions.py", line 8, in <module>
    z = y / x
ZeroDivisionError: integer division or modulo by zero

The advantage of using the logging module is that you have access to all the fancy log handlers (syslog, email, rotating file log), which is handy if you want your exception to be logged to multiple destinations.

CadentOrange
  • 3,263
  • 1
  • 34
  • 52
5

In other words,

try:
    1/0
except Exception as e:
    print e

You can get the details in the manual pages linked by Ignacio in his response.

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
2

In general avoid printing variables after exceptions by hand. If this was told to every developer, so much time would be saved. Just don't do job that can be automated.

Use a tool to upgrade your error messages instead. Once and for all.

With just one pip install you'll get this (image taken from one of the tools): enter image description here without inserting tons of prints and formats inside your code.

Have a look at traceback-with-variables, tbvaccine, pretty-errors, better-exception and other pypi tools.

Kroshka Kartoshka
  • 1,035
  • 5
  • 23
1

If you do

except AssertionError as error:
    print(error)

then that should crash your program with trace and everything as if the try/except wasn't there. But without having to change indentation and commenting out lines.

grofte
  • 1,839
  • 1
  • 16
  • 15