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

Discussion in 'Python' started by Dodo, Apr 20, 2010.

  1. Dodo

    Dodo Guest

    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
     
    Dodo, Apr 20, 2010
    #1
    1. Advertisements

  2. Dodo

    MRAB Guest

    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.
     
    MRAB, Apr 20, 2010
    #2
    1. Advertisements

  3. Dodo

    Dodo Guest

    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
     
    Dodo, Apr 20, 2010
    #3
  4. On Apr 20, 1:06 pm, MRAB <> 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?
     
    Andrej Mitrovic, Apr 20, 2010
    #4
  5. Dodo

    MRAB Guest

    Andrej Mitrovic wrote:
    > On Apr 20, 1:06 pm, MRAB <> 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:
     
    MRAB, Apr 20, 2010
    #5
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Nicholson

    Why doesn't try..catch work on my web page?

    Rob Nicholson, Mar 1, 2005, in forum: ASP .Net
    Replies:
    21
    Views:
    1,543
    Kevin Spencer
    Mar 4, 2005
  2. John Black
    Replies:
    8
    Views:
    4,512
    Xenos
    Aug 20, 2004
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    1,371
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,838
    Smokey Grindel
    Dec 2, 2006
  5. Adam
    Replies:
    9
    Views:
    914
    red floyd
    Feb 2, 2006
  6. Steve
    Replies:
    3
    Views:
    856
    Steve
    Aug 4, 2009
  7. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    818
    Daniel Pitts
    Aug 5, 2009
  8. Seth
    Replies:
    1
    Views:
    189
    Eric Hodel
    Jun 22, 2007
Loading...