[Tutor] "=" invalid syntax ?
pythontutor at venix.com
Sun Feb 1 21:36:31 EST 2004
Daniel Ehrenberg wrote:
>>try/except and try/finally are two different forms.
>>There is no try/except/finally.
>>(thanks to Alex Martelli) it should look more like:
>> fi = open('test.py', 'r')
>> except IOError:
>> print "Can't open file for reading."
>> for line in fi:
>> print line
>>The else is a convenient to limit the scope of the
>>The exceptions chapter of "Python in a Nutshell" has
>>very lucid descriptions
>>of exception handling strategies.
> I'm probably making some big mistake again, but if
> you're using both except and else, why not just write:
> fi = open('test.py', 'r')
> except IOError:
> print "Can't open file for reading."
> for line in fi:
> print line
> Daniel Ehrenberg
In actual practise, that is what I'd write. I was just showing
the general approach to having finally and except for the same
block of code.
Typically try/finally encloses a "logically" large block of
cade and simply guarantees that a log entry will get written or
that a file or other resource will get closed.
try/except usually covers small blocks of code.
It also provides general recovery control for applications that
must continue after an exception is raised.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
More information about the Tutor