35

In Python, if I open a binary file that doesn't exist, the program exits with an error and prints:

Traceback (most recent call last):
  File "C:\Python_tests\Exception_Handling\src\exception_handling.py", 
  line 4, in <module>
  pkl_file = open('monitor.dat', 'rb')
  IOError: [Errno 2] No such file or directory: 'monitor.dat'

I can handle this with 'try-except', like:

try:
    pkl_file = open('monitor.dat', 'rb')
    monitoring_pickle = pickle.load(pkl_file)
    pkl_file.close()
except Exception:
    print 'No such file or directory'

How could I, in caught Exception, print the following line?

File "C:\Python_tests\Exception_Handling\src\exception_handling.py", 
line 11, in <module>
pkl_file = open('monitor.dat', 'rb')

So the program would not exit.

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
Aleš Kancilija
  • 854
  • 2
  • 10
  • 19

4 Answers4

90

This prints the exception message:

except Exception, e:
    print "Couldn't do it: %s" % e

This will show the whole traceback:

import traceback

# ...

except Exception, e:
    traceback.print_exc()

But you might not want to catch Exception. The narrower you can make your catch, the better, generally. So you might want to try:

except IOError, e:

instead. Also on the subject of narrowing your exception handling, if you are only concerned about missing files, then put the try-except only around the open:

try:
    pkl_file = open('monitor.dat', 'rb')
except IOError, e:
    print 'No such file or directory: %s' % e

monitoring_pickle = pickle.load(pkl_file)
pkl_file.close()
Ned Batchelder
  • 323,515
  • 67
  • 518
  • 625
22

If you want to capture the exception object passed by the Exception, it's best to start using the NEW format introduced in Python 2.6 (which currently supports both) because it will be the only way to do it into Python 3.

And that is:

try:
    ...
except IOError as e:
    ...

Example:

try:
    pkfile = open('monitor.dat', 'rb')
except IOError as e:
    print 'Exception error is: %s' % e

A detailed overview can be found at the What's New in Python 2.6 documentation.

Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321
jathanism
  • 30,623
  • 9
  • 64
  • 86
9

Python has the traceback module.

import traceback
try:
    pkl_file = open('monitor.dat', 'rb')
    monitoring_pickle = pickle.load(pkl_file)
    pkl_file.close()
except IOError:
    traceback.print_exc()
Jochen Ritzel
  • 94,379
  • 28
  • 188
  • 182
6

Thanks for all.

That's, what I needed :)

import traceback

try:
    # boom
except Exception:
    print traceback.format_exc()
Aleš Kancilija
  • 854
  • 2
  • 10
  • 19
  • 8
    You shouldn't create an answer when you're making a comment. Accept one of the answers to reward your helpers! – Ned Batchelder Nov 11 '09 at 13:58
  • Please take the time to take another look at Ned B's answer. There are several important points that he makes, beyond just solving your immediate problem. – PaulMcG Nov 11 '09 at 14:05
  • Thanks, Paul, but I have a feeling we've reached the end of activity on this question... – Ned Batchelder Nov 11 '09 at 14:12