Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


rolf.oltmans at gmail

Nov 3, 2009, 5:06 AM

Post #1 of 6 (2571 views)
Permalink
Help resolve a syntax error on 'as' keyword (python 2.5)

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


kmisoft at gmail

Nov 3, 2009, 5:19 AM

Post #2 of 6 (2535 views)
Permalink
Re: Help resolve a syntax error on 'as' keyword (python 2.5) [In reply to]

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 [at] gmail> 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


contact at xavierho

Nov 3, 2009, 5:24 AM

Post #3 of 6 (2545 views)
Permalink
Re: Help resolve a syntax error on 'as' keyword (python 2.5) [In reply to]

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


deets at nospam

Nov 3, 2009, 6:00 AM

Post #4 of 6 (2536 views)
Permalink
Re: Help resolve a syntax error on 'as' keyword (python 2.5) [In reply to]

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


ben+python at benfinney

Nov 3, 2009, 6:16 AM

Post #5 of 6 (2528 views)
Permalink
Re: Help resolve a syntax error on 'as' keyword (python 2.5) [In reply to]

Oltmans <rolf.oltmans [at] gmail> 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


gagsl-py2 at yahoo

Nov 3, 2009, 4:57 PM

Post #6 of 6 (2519 views)
Permalink
Re: Help resolve a syntax error on 'as' keyword (python 2.5) [In reply to]

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.