Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to catch syntax error python [duplicate]

try:
    x===x
except SyntaxError:
    print "You cannot do that"

outputs

    x===x
       ^
SyntaxError: invalid syntax

this does not catch it either

try:
    x===x
except:
    print "You cannot do that"

Other errors like NameError, ValueError, are catchable.

Thoughts?

System specs:

import sys
print(sys.version)

-> 2.7.5 (default, Mar 9 2014, 22:15:05) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)]

like image 353
humanbeing Avatar asked Jul 31 '14 01:07

humanbeing


People also ask

How do I capture a syntax error in Python?

A SyntaxError occurs any time the parser finds source code it does not understand. This can be while importing a module, invoking exec, or calling eval(). Attributes of the exception can be used to find exactly what part of the input text caused the exception.

Can we handle syntax error in Python?

You can't handle invalid syntax in Python like other exceptions. Even if you tried to wrap a try and except block around code with invalid syntax, you'd still see the interpreter raise a SyntaxError .

What are the 3 major exception types in Python?

There are mainly three kinds of distinguishable errors in Python: syntax errors, exceptions and logical errors.

Can you catch a syntax error?

You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors. The try block must be followed by either exactly one catch block or one finally block (or one of both). When an exception occurs in the try block, the exception is placed in e and the catch block is executed.


2 Answers

You can only catch SyntaxError if it's thrown out of an eval, exec, or import operation.

>>> try: ...    eval('x === x') ... except SyntaxError: ...    print "You cannot do that" ...  You cannot do that 

This is because, normally, the interpreter parses the entire file before executing any of it, so it detects the syntax error before the try statement is executed. If you use eval or its friends to cause more code to be parsed during the execution of the program, though, then you can catch it.

I'm pretty sure this is in the official manual somewhere, but I can't find it right now.

like image 76
zwol Avatar answered Oct 14 '22 22:10

zwol


SyntaxErrors get raised when the file/code is parsed, not when that line of code is executed. The reason for this is simple -- If the syntax is wrong at a single point in the code, the parser can't continue so all code after that line is un-parseable.

In other words, you can only catch syntax errors when python is trying to parse something. This includes exec, eval, import:

>>> try:
...    import junk
... except SyntaxError:
...    print "Woo"
... 
Woo

and various things regarding ast and the like.

Note that the python tutorial even distinguishes between SyntaxError and other exceptions although the distinction isn't as clear as the tutorial makes it seem (since you can in fact catch SyntaxError if you know when they get raised).

like image 31
mgilson Avatar answered Oct 15 '22 00:10

mgilson