A bug or a feature: how to avoid?

sismex01 at hebmex.com sismex01 at hebmex.com
Tue Nov 26 11:26:32 EST 2002


> From: Lexy Zhitenev [mailto:zhitenev at cs.vsu.ru]
> Sent: Tuesday, November 26, 2002 10:13 AM
> 
> My code has to react on many exceptions.
> I use ActivePython 2.2.1 with stackless 2.2.2 DLL.
> 
> The problem is that I can't write a handler for more than two 
> exceptions at once. My program can throw a lot of exceptions,
> and I can't react on them simultaneously!
> 
> 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?
>

use:

   try:
      pass
   except (IndexError,ValueError,NameError), err:
      do_something()

The exceptions to be caught should be either a single class
or a tuple of classes.

Good luck!

-gustavo




More information about the Python-list mailing list