try/except/else flexibility...

Alex Martelli aleax at aleax.it
Fri Apr 12 03:12:09 EDT 2002


Magnus Lie Hetland wrote:
        ...
>   try: foo + ''
>   else: bar()
> 
> and if I want something done only if it causes no exceptions, for
> instance pop off an element of a list unless it is empty, I could just
> do:
> 
>   try: somelist.pop()
> 
> Is this heresy? I see the harm in not specifying exactly which
> exceptions you're expecting, of course -- are there other arguments
> against this? (I'm sure that might be argument enough... Oh, well...)

It seems to me that forgetting or mis-aligning an except or finally
clause is an error that these language changes would make it harder
to diagnose.  I'm an enthusiast for "easy to ask forgiveness than
permission", but this way you would make it easiest and most
natural to not even ask permission.  I think this would take us
back too close to (say) the typical problem with C -- system calls
or library functions carefully giving out error codes, and bulldozing
application programmers totally ignoring those codes and doing no
error-checking at all.  One advantage of exceptions is that the
default is NOT to ignore -- and the ease to just put everything in 
a blind 'try:' might, I fear, claw back most of this advantage,


Alex




More information about the Python-list mailing list