Help resolve a syntax error on 'as' keyword (python 2.5)
Ben Finney
ben+python at benfinney.id.au
Tue Nov 3 09:16:47 EST 2009
Oltmans <rolf.oltmans at gmail.com> 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
More information about the Python-list
mailing list