A bug or a feature: how to avoid?

Daniel Dittmar daniel.dittmar at sap.com
Tue Nov 26 11:40:50 EST 2002


Lexy Zhitenev wrote:
>>>> try:
> ...  pass
> ... except IndexError, ValueError, NameError:
> Traceback (  File "<interactive input>", line 3
>     except IndexError, ValueError, NameError:
>                                  ^
> SyntaxError: invalid syntax
>>>>
>
> I don't want to write the code under try statement twice! What's the
> solution? Or it should be reported as a bug?

Hidden on http://www.python.org/doc/current/ref/try.html are the words
>>>
An object is compatible with an exception if it is either the object that
identifies the exception, or (for exceptions that are classes) it is a base
class of the exception, or it is a TUPLE CONTAINING AN ITEM that is
compatible with the exception.
<<<
So using
except (IndexError, ValueError, NameError), exceptionVar:
should do the trick.

Daniel





More information about the Python-list mailing list