"Try:" which only encompasses head of compound statement
Bjoern Schliessmann
usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Mon Aug 27 17:45:40 EDT 2007
Jameson.Quinn at gmail.com wrote:
> I have:
>
> try:
> for line in open(myFileName):
> count += 1
> except IOError:
> print "Can't open myfile"
>
> (I know, this is bad, I never close the file, but its just for
> illustration). But then I change it to:
>
> try:
> for line in open(myFileName):
> count += openAndProcessSubfile(line)
> except IOError:
> print "Can't open myfile"
>
> ... now the 'except' incorrectly catches errors from
> openAndProcessSubfile.
The typical way to counter this would be by catching the IOError
earlier so it won't propagate to the outmost "try":
try:
for line in open(myFileName):
try:
count += openAndProcessSubfile(line)
except IOError:
print "Can't open subfile"
except IOError:
print "Can't open myfile"
Regards,
Björn
--
BOFH excuse #121:
halon system went off and killed the operators.
More information about the Python-list
mailing list