NameError

Built-in ExceptionPython 2.0+Beginner

Raised when a variable name is not found in scope

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
# Triggering and catching NameError
try:
    print(undefined_variable)
except NameError as e:
    print(f"Caught NameError: {e}")
    print(f"Type: {type(e).__name__}")

NameError is raised when raised when a variable name is not found in scope. Always catch specific exceptions rather than bare except clauses.

Try in Playground

Tags

exceptionerror-handlingcore