How to Catch 2 Exceptions at once?

Steve Holden steve at
Sun Oct 1 03:43:20 CEST 2006

Gregory Piñero wrote:
> How can I catch 2 exceptions at once for example:
>         try:
>             self.gses =
>         except (urllib2.HTTPError,urllib2.URLError):
>             do something..
> Seems to work, but how do I also get information about the error?
As usual, by adding an additional name after the exception specification:

             self.gses =
         except (urllib2.HTTPError,urllib2.URLError), exdata:
             do something with exdata ...

Pedantically speaking, of course, you aren't catching two exceptions at 
once because they don't occur simultaneously: you are writing a 
statement that catches either of two exceptions.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd
Skype: holdenweb
Recent Ramblings

More information about the Python-list mailing list