[Python-ideas] with statement vs. try...except...finally

Georg Brandl g.brandl at gmx.net
Wed Jun 3 09:11:36 CEST 2009


Nick Coghlan schrieb:

> def get_first_line(fname):
>   try:
>     f = open('foo')
>   except IOError:
>     line = "can't open"
>   try:
>     line = f.readline()
>   except IOError:
>     line = "can't read"
>   try:
>     f.close()
>   except IOError:
>     line = "can't close"
>   return line
> 
> line = get_first_line('foo')

Anyway, doesn't it make more sense to report not what *operation*
failed, but what the *failure* was (the specifics of the IOError)?

Georg




More information about the Python-ideas mailing list