On Tue, Nov 3, 2009 at 4:06 PM, Oltmans <rolf.oltm...@gmail.com> wrote: > Hi, all. All I'm trying to do is to print the error message using the > following code (copying/pasting from IDLE).
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.
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.