Help resolve a syntax error on 'as' keyword (python 2.5)

Discussion in 'Python' started by Oltmans, Nov 3, 2009.

  1. Oltmans

    Oltmans Guest

    Hi, all. All I'm trying to do is to print the error message using the
    following code (copying/pasting from IDLE).

    def div(a,b):
    print a/b


    try:
    div(5,0)
    except Exception as msg:
    print msg

    but IDLE says (while highlighting the 'as' keyword)
    except Exception as msg:

    SyntaxError: invalid syntax

    I've searched the internet and I'm not sure what can cause this. Any
    help is highly appreciated. I'm using Python 2.5 on Windows XP.
    Oltmans, Nov 3, 2009
    #1
    1. Advertising

  2. Hi,

    "except Exception as variable"

    is a new python-3 syntax.

    You should use "except Exception, variable" syntax in 2.x series.

    Vladimir Ignatov



    On Tue, Nov 3, 2009 at 4:06 PM, Oltmans <> wrote:
    > Hi, all. All I'm trying to do is to print the error message using the
    > following code (copying/pasting from IDLE).
    >
    > def div(a,b):
    >        print a/b
    >
    >
    > try:
    >    div(5,0)
    > except Exception as msg:
    >    print msg
    >
    > but IDLE says (while highlighting the 'as' keyword)
    > except Exception as msg:
    >
    > SyntaxError: invalid syntax
    >
    > I've searched the internet and I'm not sure what can cause this. Any
    > help is highly appreciated. I'm using Python 2.5 on Windows XP.
    Vladimir Ignatov, Nov 3, 2009
    #2
    1. Advertising

  3. Vladimir Ignatov wrote:

    > Hi,
    >
    > "except Exception as variable"
    >
    > is a new python-3 syntax.
    >
    > You should use "except Exception, variable" syntax in 2.x series.


    Not entirely true. This feature has been backported to python2.6 as well.

    Diez
    Diez B. Roggisch, Nov 3, 2009
    #3
  4. En Tue, 03 Nov 2009 10:06:24 -0300, Oltmans <>
    escribió:

    > Hi, all. All I'm trying to do is to print the error message using the
    > following code (copying/pasting from IDLE).
    >
    > try:
    > div(5,0)
    > except Exception as msg:
    > print msg
    >
    > SyntaxError: invalid syntax
    >
    > I'm using Python 2.5 on Windows XP.


    Other people already told you what the problem is.
    I suggest reading a book/tutorial written for the *same* Python version
    you're using (2.x; it doesn't matter 2.6, 2.5, 2.4...).
    Once you know the basics of the language, you may look at the differences
    in the "What's new?" document for Python 3.0 - but right now, they will
    just confuse you.

    --
    Gabriel Genellina
    Gabriel Genellina, Nov 4, 2009
    #4
    1. Advertising

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. TJS
    Replies:
    2
    Views:
    462
    Steve C. Orr [MVP, MCSD]
    Jan 16, 2004
  2. Replies:
    1
    Views:
    10,147
  3. Replies:
    6
    Views:
    452
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    350
  5. galopin
    Replies:
    0
    Views:
    304
    galopin
    May 9, 2009
Loading...

Share This Page