Why this exception catch doesn't work?? (python 3)

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Apr 20 09:23:37 EDT 2010


On Apr 20, 1:06 pm, MRAB <pyt... at mrabarnett.plus.com> wrote:
> Dodo wrote:
> > Hello,
>
> > I don't understand why this won't execute
>
> > import urllib.request as u
> > import socket
> > socket.setdefaulttimeout(10)
>
> > l = "http://img144.imageshack.us/my.php?image=koumakandg8.jpg" #
> > supposed to timeout
> > try:
> >     h = u.urlretrieve(l)
> > except u.URLError, e: # I tried u.e too, no effect.
> >     print(e)
> > except:
> >     print("other error")
>
> > The error :
>
> > ...\Python>err.py
> >   File "...\err.py", line 8
> >     except u.URLError, e: # I tried u.e too, no effect.
> >                      ^
> > SyntaxError: invalid syntax
>
> In Python 3 it's:
>
>      except u.URLError as e:
>
> This a because in Python 2 people sometimes write:
>
>      except OSError, IOError:
>
> thinking that it will catch both OSError and IOError.

except (OSError, IOError), e:  # Python 2.x

If you put them in a tuple, it will catch them, right?



More information about the Python-list mailing list