try...except...finally problem in Python 2.5
Andrew Koenig
ark at acm.org
Wed Feb 14 14:46:14 EST 2007
"redawgts" <redawgts at gmail.com> wrote in message
news:1171482088.993720.69620 at l53g2000cwa.googlegroups.com...
>I keep getting this error "local variable 'f' referenced before
> assignment" in the finally block when I run the following code.
>
> try:
> f = file(self.filename, 'rb')
> f.seek(DATA_OFFSET)
> self.__data = f.read(DATA_SIZE)
> self.isDataLoaded = True
> except:
> self.isDataLoaded = False
> finally:
> f.close()
>
> Can someone tell me what's wrong with the code? Am I doing something
> wrong? I'm somewhat new to python but this makes sense to me.
If the call to file raises an exception, then variable f won't have been
created.
Here's a simple example:
def f():
throw 42
try:
x = f()
except:
print x
Try it and see what happens. While you're at it, you might try to figure
out what you would like it to print.
More information about the Python-list
mailing list