[Python-ideas] why not try without except?

Ben Finney ben+python at benfinney.id.au
Sat Apr 25 12:34:36 CEST 2009


spir <denis.spir at free.fr> writes:

> Well, i think my example circle drawing func answers your question. It
> does not propagate the exception, instead precisely avoids this.

>     wibble()
>     try:
>         foo()
>         bar()
>         baz()
>     wobble()
> <==>
>     wibble()
>     try:
>         foo()
>         bar()
>         baz()
>     except Exception:
>        pass
>     wobble()

Thank you for explaining.

I am solidly against this proposal, as I think it's a pattern that
should be discouraged, not accommodated.

> Another example:
>    result = ...
>    option result.applyTransforms(transforms)
> <==>
>    result = ...
>    try:
>       option result.applyTransforms(transforms)
>    except Exception:
>       pass

Can you show some real-world code that would be improved by this? I
don't know of any code where *all* exceptions, especially unexpected
ones, should simply be thrown away.

If there are specific exception types that should be discarded, it is
better to list them explicitly, and make sure your code is very clear on
*why* those specific exceptions are being discarded.

If you want exceptions to be handled in a generic way, it is better to
set up a custom exception handler.

-- 
 \       “If you are unable to leave your room, expose yourself in the |
  `\            window.” —instructions in case of fire, hotel, Finland |
_o__)                                                                  |
Ben Finney




More information about the Python-ideas mailing list