SystemExit — Easy Examples
Raised by sys.exit(); used to exit the interpreter
Triggering SystemExit
How SystemExit is raised and how to catch it.
python
# Triggering and catching SystemExit try: raise SystemExit(0) except SystemExit as e: print(f"Caught SystemExit: {e}") print(f"Type: {type(e).__name__}")
SystemExit is raised when raised by sys.exit(); used to exit the interpreter. Always catch specific exceptions rather than bare except clauses.
Handling SystemExit
Basic error handling pattern for SystemExit.
python
# Safe handling pattern def safe_operation(): try: raise SystemExit(0) except SystemExit: 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