Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Help resolve a syntax error on 'as' keyword (python 2.5)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Oltmans  
View profile  
 More options Nov 4, 12:06 am
Newsgroups: comp.lang.python
From: Oltmans <rolf.oltm...@gmail.com>
Date: Tue, 3 Nov 2009 05:06:24 -0800 (PST)
Local: Wed, Nov 4 2009 12:06 am
Subject: 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.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vladimir Ignatov  
View profile  
 More options Nov 4, 12:19 am
Newsgroups: comp.lang.python
From: Vladimir Ignatov <kmis...@gmail.com>
Date: Tue, 3 Nov 2009 16:19:15 +0300
Local: Wed, Nov 4 2009 12:19 am
Subject: Re: Help resolve a syntax error on 'as' keyword (python 2.5)
Hi,

"except Exception as variable"

is a new python-3 syntax.

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

Vladimir Ignatov


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Diez B. Roggisch  
View profile  
 More options Nov 4, 1:00 am
Newsgroups: comp.lang.python
From: "Diez B. Roggisch" <de...@nospam.web.de>
Date: Tue, 03 Nov 2009 15:00:23 +0100
Local: Wed, Nov 4 2009 1:00 am
Subject: Re: Help resolve a syntax error on 'as' keyword (python 2.5)

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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Finney  
View profile  
 More options Nov 4, 1:16 am
Newsgroups: comp.lang.python
From: Ben Finney <ben+pyt...@benfinney.id.au>
Date: Wed, 04 Nov 2009 01:16:47 +1100
Local: Wed, Nov 4 2009 1:16 am
Subject: Re: Help resolve a syntax error on 'as' keyword (python 2.5)

Oltmans <rolf.oltm...@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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Nov 4, 11:57 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Tue, 03 Nov 2009 21:57:00 -0300
Subject: Re: Help resolve a syntax error on 'as' keyword (python 2.5)
En Tue, 03 Nov 2009 10:06:24 -0300, Oltmans <rolf.oltm...@gmail.com>  
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google