Cathing several potential errors?

John Machin sjmachin at lexicon.net
Sat Sep 6 18:42:37 EDT 2008


On Sep 7, 8:13 am, cnb <circularf... at yahoo.se> wrote:
> if i do
> try:
>     something
> except TypeError, IndexError:
>     pass
>
> only the first error will get caught. I dont want to use Exception and
> catch all errors, but just 2. how can i do that?

The syntax for what you can have between "except" and ":" is:
    [expression  ["," target]]
'target' is an optional name to be bound to the actual exception
object that is raised.
If you want to catch multiple kinds of exception, 'expression' needs
to be a tuple.

See
http://docs.python.org/tut/node10.html#SECTION0010300000000000000000
and
http://docs.python.org/ref/try.html

Cheers,
John



More information about the Python-list mailing list