Usobi
Usobi

Reputation: 1856

try except syntax error

I have a problem with the next code.

try: 1+1
except Exception as exception: pass
1+1
try: 2+2
except Exception as exception: pass

The result I get in the prompt is

... ...   File "<stdin>", line 3
    1+1
    ^
SyntaxError: invalid syntax
>>> ... ... ... 4

However the next code executes with no error.

try: 1+1
except Exception as exception: pass

try: 2+2
except Exception as exception: pass

My sys.version_info is:

sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)

Why do I get the syntax error?

Upvotes: 3

Views: 1344

Answers (1)

MattDMo
MattDMo

Reputation: 102852

When using the interactive prompt, there needs to be a blank line between a block (such as a try/except block) and the next independent command. This is only in the REPL, when running a .py file it's not necessary.

Upvotes: 5

Related Questions