[Python-ideas] why not try without except?

spir denis.spir at free.fr
Sat Apr 25 12:23:22 CEST 2009


Le Sat, 25 Apr 2009 19:12:11 +1000,
Ben Finney <ben+python at benfinney.id.au> s'exprima ainsi:


> But there *is* no condition in a ‘try’ statement.
> 
> If what you mean is “when the suite introduced by ‘try:’ may raise an
> exception”, that's true of just about every interesting statement in
> Python: an exception might be raised at any point.

You're right, that's what I mean.

> > Both express an optional action.
> 
> No. The suite that follows ‘try’ will be executed, just as surely as a
> suite *not* enclosed in ‘try’.

It's a question of point of view. From the language side, you're certainly right. But from the programmer side, I guess it's correct to say that "try... do_this... except ExceptionType... pass" expresses an optional action. Precisely because there is no alternative. It's like an if without else.

> > This could be written more directly, because we do not need the condition:
> >    ? <do_something>
> >    option <do_something>
> > with the meaning: "Try & do this, but if there's an exception just let
> >    it down."
> 
> What does “let it down” mean?

It means more or less:
   except Exception:
      pass

> If you mean “if there's an exception raised, just let it propagate up”,
> that's what happens *without* a ‘try’. So I can only assume you mean
> something different.

Yes, see above.

> I don't understand the behaviour you describe. Perhaps if you would
> explain what you think the difference should be between:
> 
>     wibble()
>     try:
>         foo()
>         bar()
>         baz()
>     wobble()
> 
> versus this:
> 
>     wibble()
>     foo()
>     bar()
>     baz()
>     wobble()
> 
> How should the behaviour differ?
> 
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()

But I prefere the 'option' syntax not only because it expresses the intention in a clearler and more direct way; also because it applies on a single statement -- which avoids potential some misuse like may happen in your example: try with several statements can more easily catch unexpected errors -- but this happens with standard try...except too.

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

Now, I agree with the critics in another reply that
   except Exception:
      pass
is very different of
   except NameError:
      pass

We could probably find a way to specify the type of exception:
   option result.applyTransforms(transforms) except NameError

Denis
------
la vita e estrany



More information about the Python-ideas mailing list