A bug or a feature: how to avoid?

Duncan Booth duncan at rcp.co.uk
Tue Nov 26 11:34:27 EST 2002


"Lexy Zhitenev" <zhitenev at cs.vsu.ru> wrote in
news:as06i3$4bi$1 at serv.vrn.ru: 

> This code doesn't work, it raises SyntaxError
> 
>>>> 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?

No bug. Put the exceptions in parentheses:
>>> try:
	pass
except (IndexError, ValueError, NameError):
	pass

The first parameter to except is either a single exception or a tuple of
exceptions, but this is one of the situations where the parentheses are
needed around the tuple. 



More information about the Python-list mailing list