简体   繁体   中英

Google Appengine and Python exceptions

In my Google Appengine application I have defined a custom exception InvalidUrlException(Exception) in the module 'gvu'. Somewhere in my code I do:

try:
    results = gvu.article_parser.parse(source_url)
except gvu.InvalidUrlException as e:
    self.redirect('/home?message='+str(e))
...

which works fine in the local GAE development server, but raises

<type 'exceptions.SyntaxError'>: invalid syntax (translator.py, line 18)

when I upload it. (line 18 is the line starting with 'except')

The problem seems to come from the 'as e' part: if I remove it I don't get this exception anymore. However I would like to be able to access the raised exception. Have you ever encountered this issue? Is there an alternative syntax?

You probably have an older Python version on your server. except ExceptionType as varname: is a newer syntax. Previously you needed to simply use a comma: except ExceptionType, varname: .

我遇到了同样的错误,因为我在使用python3打印语句(带括号的打印语句)的python3文件上使用pydoc命令而不是pydoc3命令。

Just FYI, another possible cause for this error - especially if the line referenced is early in the script (like line 2) is line ending differences between Unix and Windows.

I was running Python on Windows from a Cygwin shell and got this error, and was really puzzled. I had created the file with "touch" before editing it.

I renamed the file to a temp file name, and copied another file (which I had downloaded from a Unix server) onto the original file name, then restored the contents via the temp file, and problem solved. Same exact file contents (on the screen anyway), only difference was where the file had originally been created.

Just wanted to post this in case anyone else ran across this error and was similarly puzzled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM