why the code error?

Noah noah at noah.org
Mon May 20 02:09:00 EDT 2002


The exception syntax you used is correct, but it does not
assign the exception to a variable.
You just need to add a variable to your except statement.
For the except section you want it to look like this:
    except IOError, e:
        print str(e)
        return
So the way you used it you cannot get specific information
about the exception.

In addition you could import traceback and print the stack.
This may give you more interesting information.
It would look something like this:
    import traceback
    ...
    try:
        ...
    except IOError, e:
        print str(e)
        traceback.print_exc()
        return

Yours,
Noah


-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of lunxian
Sent: Sunday, May 19, 2002 7:50 PM
To: python-list at python.org
Subject: why the code error?


i'm a newbie of python. i encounter a problem in my programming.
here is my program. when it's running, a IOError always raised.
why? and how can i get more detail error message of the exception?


import os
import sys

def Parser(filename):
    lines = None
    try: 
        fsock = file("forparser.py","r",0)
        try: 
            lines = fsock.readlines() 
        finally: 
            fsock.close() 
    except IOError:
        print IOError.__str__
        return

    for line in lines:
        print line

if __name__ == "__main__":
    Parser("c:\forparser.py")






More information about the Python-list mailing list