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

D

Dodo

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


Dorian
 
M

MRAB

Dodo said:
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.
 
D

Dodo

Le 20/04/2010 13:06, MRAB a écrit :
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.

thanks =D
 
A

Andrej Mitrovic

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?
 
M

MRAB

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

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

In Python 2.x:

except (OSError, IOError), e:

In Python 3.x (and also Python 2.6):

except (OSError, IOError) as e:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top