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

View: New views
6 Messages — Rating Filter:   Alert me  

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

by Oltmans :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.
--
http://mail.python.org/mailman/listinfo/python-list

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

by Vladimir Ignatov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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 <rolf.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.
--
http://mail.python.org/mailman/listinfo/python-list

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

by Xavier Ho :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't have Python 25 on my computer, but since codepad.org is using Python 2.5.1, I did a quick test:

# input
try:
    raise Exception("Mrraa!")
except Exception as msg:
    print msg

# output
t.py:3: Warning: 'as' will become a reserved keyword in Python 2.6
  Line 3
    except Exception as msg:
                      ^
SyntaxError: invalid syntax

#########

Well that's interesting. as isn't a keyword yet in Python 2.5.

This works though, and I think it fits the specification in the documentation.

# input
try:
    raise Exception("Mrraa!")
except Exception, msg:  # Notice it's a comma
    print msg

# output
Mrraa!

##############

Hope that helps,
Xav

--
http://mail.python.org/mailman/listinfo/python-list

Parent Message unknown Re: Help resolve a syntax error on 'as' keyword (python 2.5)

by Diez B. Roggisch-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
--
http://mail.python.org/mailman/listinfo/python-list

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

by Ben Finney-10 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oltmans <rolf.oltmans@...> writes:

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

The name ‘msg’ here is misleading. The except syntax does *not* bind the
target to a message object, it binds the target to an exception object.

It would be clearer to write the above code as:

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

since the target ‘exc’ names an exception object, not a message. (The
‘print’ function will *create* a string object from the exception
object, use the string, then discard it.)

> 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.

When you get a syntax error, you should check the syntax documentation
for the version of Python you're using.

> Any help is highly appreciated. I'm using Python 2.5 on Windows XP.

The syntax above is for Python 3 only
<URL:http://docs.python.org/3.1/reference/compound_stmts.html#try>.

In Python 2.5.4, the ‘try … except’ syntax was different
<URL:http://www.python.org/doc/2.5.4/ref/try.html>, and ‘print’ was not
implemented as a function, but instead as a keyword
<URL:http://www.python.org/doc/2.5.4/ref/print.html>. Use this form:

    try:
        div(5, 0)
    except Exception, exc:
        print exc

--
 \       “When I get new information, I change my position. What, sir, |
  `\             do you do with new information?” —John Maynard Keynes |
_o__)                                                                  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

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

by Gabriel Genellina-7 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

En Tue, 03 Nov 2009 10:06:24 -0300, Oltmans <rolf.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

--
http://mail.python.org/mailman/listinfo/python-list