RecursionError — Easy Examples
Raised when the maximum recursion depth is exceeded
Triggering RecursionError
How RecursionError is raised and how to catch it.
python
# Triggering and catching RecursionError try: def f(): f() f() except RecursionError as e: print(f"Caught RecursionError: {e}") print(f"Type: {type(e).__name__}")
RecursionError is raised when raised when the maximum recursion depth is exceeded. Always catch specific exceptions rather than bare except clauses.
Handling RecursionError
Basic error handling pattern for RecursionError.
python
# Safe handling pattern def safe_operation(): try: def f(): f() f() except RecursionError: print("Operation failed gracefully") return None result = safe_operation() print(f"Result: {result}")
Wrapping risky operations in try/except blocks prevents your program from crashing.
Want to try these examples interactively?
Open Easy Playground