SyntaxError

Built-in ExceptionPython 2.0+Beginner

Raised when the parser encounters invalid Python syntax

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching SyntaxError
try:
    exec("if True print('hi')")
except SyntaxError as e:
    print(f"Caught SyntaxError: {e}")
    print(f"Type: {type(e).__name__}")

SyntaxError is raised when raised when the parser encounters invalid python syntax. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore