[Python-3000] Exception Expressions

Nick Coghlan ncoghlan at gmail.com
Sat Sep 2 05:42:45 CEST 2006

An interesting idea, although I suspect a leading try keyword would make 
things clearer.

   (try expr1 except expr2 if exc_type)

print (try letters[7] except "N/A" if IndexError)
f = (try open(filename) except open(filename2) if IOError)
print (try eval(expr) except "Can not divide by zero!" if ZeroDivisionError)
val = (try db.get(key) except cache.get(key) if TimeoutError)

This wouldn't help the chaining problem that Greg pointed out, though:

try open(name1) except (try open(name2) except open(name3) if IOError) if IOError

Using a different keyword or a comma so expr2 comes last as Greg suggested 
would fix that:

try open(name1) except IOError, (try open(name2) except IOError, open(name3))

I'd be somewhere between -1 and -0 at this point in time. Depending on the 
results a review of the standard library describing actual use cases that 
could be made easier to read might be enough to get me to a +0.


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-3000 mailing list