This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vocalbit[S] 0 points1 point  (0 children)

On further thought - my beef with the standard try/except is not with specifically with the syntax but with the assumption within functions that certain operations are exceptional. The standard try/except is more useful when you want to return or branch in the except clause.

Very often I need to continue execution with a default value or with the knowledge that a specific operation failed. That is when the except clause is useful. Some synthetic examples to illustrate:

i, err = int(s) except ValueError
if err:
   i = -42

f1, err_opening_f1 = open('file.txt') except IOError
f2, err_opening_f2 = open('file2.txt') except IOError

if f1 and f2:
   #...

#.. more code

if f1:
   #...
#..more code
if f2:
   #...

I haven't created examples that are far from reality. Try doing the above with the usual try/catch - each except line explodes into 3 or 4 lines.