Exception message (Python 2.6)

后端 未结 4 1258
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 10:02

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_te         


        
4条回答
  •  隐瞒了意图╮
    2020-12-05 10:21

    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.

提交回复
热议问题