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

O

Oltmans

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

Vladimir Ignatov

Hi,

"except Exception as variable"

is a new python-3 syntax.

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

Vladimir Ignatov
 
D

Diez B. Roggisch

Vladimir said:
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
 
G

Gabriel Genellina

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top