tags:

views:

99

answers:

3

Hello!

I'm using Django, and I have the following error:

Exception Type: SyntaxError Exception Value: invalid syntax (views.py, line 115)

My viws.py code looks like this:

def myview(request):
try:
 [...]
except MyExceptionClass, e:
 [...]
finally:
 render_to_response('template.html', {}, context_instance = RequestContext(request))

Where MyExceptionClass is a class extending Exception, and line 115 is the 'finally' clause line. If I remove finally clause, (lines 115-116), works fine. Any idea?

Thanks a lot!

A: 

In Python 3, should be:

except MyExceptionClass as e:
    [....]

In your case, this is not the case.

Yuval A
Only in Python 3 -- in 2.5, the original poster's syntax is right.
Alex Martelli
woops, you're right...
Yuval A
+5  A: 

What version of python are you using? Prior to 2.5 you can't have both an except clause and a finally clause in the same try block.

You can work around this by nesting try blocks.

def myview(request):
    try:
        try:
            [...]
        except MyExceptionClass, e:
            [...]
    finally:
        render_to_response(
            'template.html', {}, context_instance = RequestContext(request)
        )
Nadia Alramli
That's right! I'm using Python 2.4.4Thanks a lot Nadia!
+1  A: 

Nadia is right, so if you're stuck with Python 2.4 or earlier, use two try blocks:

try:
  try:
        [...]
  except MyExceptionClass, e:
        [...]
finally:
        render_to_response(...)
Alex Martelli