catching exceptions from an except: block

Gerard Flanagan grflanagan at yahoo.co.uk
Fri Mar 9 05:30:20 EST 2007


On Mar 9, 9:56 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Fri, 09 Mar 2007 04:49:59 -0300, Gerard Flanagan
> <grflana... at yahoo.co.uk> escribió:
>
> > Another version:
>
> > import exceptions
>
> As back in time as I could go (Python 1.5), exceptions were available as
> builtins...
>

I did not know that. Thanks.

> > def onfailFalse(fn):
> >     def inner(*args, **kwargs):
> >         try:
> >             return fn(*args, **kwargs)
> >         except ABCException:
> >             return False
> >     return inner
>
> > @onfailFalse
> > def a(x):
> >     if x == 1:
> >         return 'function a succeeded'
> >     else:
> >         raise ABCException()
>
> There is a serious flaw on this approach, the function can't return any
> false value (it would be treated as a failure).
>

I was teaching myself decorators more than anything, so it's not
thought out to any extent, but even so I don't think it's a "serious
flaw", rather it would be programmer error to use @onfailFalse on a
function that may return False. Don't you think?

Gerard






More information about the Python-list mailing list