Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why this exception catch doesn't work?? (python 3)

Reply
Thread Tools

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

 
 
Dodo
Guest
Posts: n/a
 
      04-20-2010
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
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      04-20-2010
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.
 
Reply With Quote
 
 
 
 
Dodo
Guest
Posts: n/a
 
      04-20-2010
Le 20/04/2010 13:06, MRAB a écrit :
> 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.


thanks =D
 
Reply With Quote
 
Andrej Mitrovic
Guest
Posts: n/a
 
      04-20-2010
On Apr 20, 1:06*pm, MRAB <(E-Mail Removed)> 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?
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      04-20-2010
Andrej Mitrovic wrote:
> On Apr 20, 1:06 pm, MRAB <(E-Mail Removed)> 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?


In Python 2.x:

except (OSError, IOError), e:

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

except (OSError, IOError) as e:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
Why can not catch the inner exception =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7zS?= Python 2 06-07-2007 04:01 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Try-catch not working - why does this throw an Exception? Jon Maz ASP .Net 7 10-25-2004 04:55 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM



Advertisments