65
votes

I'm using python to evaluate some measured data. Because of many possible results it is difficult to handle or possible combinations. Sometimes an error happens during the evaluation. It is usually an index error because I get out of range from measured data.

It is very difficult to find out on which place in code the problem happened. It would help a lot if I knew on which line the error was raised. If I use following code:

try:
    result = evaluateData(data)
except Exception, err:
    print ("Error: %s.\n" % str(err))

Unfortunately this only tells me that there is and index error. I would like to know more details about the exception (line in code, variable etc.) to find out what happened. Is it possible?

Thank you.

7
@JeCh The answers look good. Please accept one. To accept it click the empty checkmark next to the answer. - Be Kind To New Users
The answer that is voted highest here is also presented in the stackoverflow question that @CharlesBeattie references above. That answer has important comments associated with it whereas this answer does not. It's definitely worth your time to check that one out. - harperville

7 Answers

91
votes

Solution, printing filename, linenumber, line itself and exception descrpition:

import linecache
import sys

def PrintException():
    exc_type, exc_obj, tb = sys.exc_info()
    f = tb.tb_frame
    lineno = tb.tb_lineno
    filename = f.f_code.co_filename
    linecache.checkcache(filename)
    line = linecache.getline(filename, lineno, f.f_globals)
    print 'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj)


try:
    print 1/0
except:
    PrintException()

Output:

EXCEPTION IN (D:/Projects/delme3.py, LINE 15 "print 1/0"): integer division or modulo by zero
36
votes

To simply get the line number you can use sys, if you would like to have more, try the traceback module.

import sys    
try:
    [][2]
except IndexError:
    print("Error on line {}".format(sys.exc_info()[-1].tb_lineno))

prints:

Error on line 3

Example from the traceback module documentation:

import sys, traceback

def lumberjack():
    bright_side_of_death()

def bright_side_of_death():
    return tuple()[0]

try:
    lumberjack()
except IndexError:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    print "*** print_tb:"
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    print "*** print_exception:"
    traceback.print_exception(exc_type, exc_value, exc_traceback,
                              limit=2, file=sys.stdout)
    print "*** print_exc:"
    traceback.print_exc()
    print "*** format_exc, first and last line:"
    formatted_lines = traceback.format_exc().splitlines()
    print formatted_lines[0]
    print formatted_lines[-1]
    print "*** format_exception:"
    print repr(traceback.format_exception(exc_type, exc_value,
                                          exc_traceback))
    print "*** extract_tb:"
    print repr(traceback.extract_tb(exc_traceback))
    print "*** format_tb:"
    print repr(traceback.format_tb(exc_traceback))
    print "*** tb_lineno:", exc_traceback.tb_lineno
16
votes

I use the traceback which is simple and robust:

import traceback

try:
    raise ValueError()
except:
    print(traceback.format_exc())

Out:

Traceback (most recent call last):
  File "catch.py", line 4, in <module>
    raise ValueError()
ValueError
13
votes

The simplest way is just to use:

import traceback
try:
    <blah>
except IndexError:
    traceback.print_exc()

or if using logging:

import logging
try:
    <blah>
except IndexError as e:
    logging.exception(e)
1
votes

I would suggest using the python logging library, it has two useful methods that might help in this case.

  1. logging.findCaller()

    • findCaller(stack_info=False) - Reports just the line number for the previous caller leading to the exception raised
    • findCaller(stack_info=True) - Reports the line number & stack for the previous caller leading to the exception raised
  2. logging.logException()

    • Reports the line & stack within the try/except block that raised the exception

For more info checkout the api https://docs.python.org/3/library/logging.html

1
votes

Gives you file, lineno, and exception for the last item in the call stack

from sys import exc_info
from traceback import format_exception


def print_exception():
    etype, value, tb = exc_info()
    info, error = format_exception(etype, value, tb)[-2:]
    print(f'Exception in:\n{info}\n{error}')

try:
    1 / 0
except:
    print_exception()

prints

Exception in:
   File "file.py", line 12, in <module>
    1 / 0

ZeroDivisionError: division by zero

0
votes

I always use this snippet

import sys, os

try:
    raise NotImplementedError("No error")
except Exception as e:
    exc_type, exc_obj, exc_tb = sys.exc_info()
    fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
    print(exc_type, fname, exc_tb.tb_lineno)

for different views and possible issues you can refer When I catch an exception, how do I get the type, file, and line number?