__debug__Easy Examples

True under normal execution; False when run with -O flag

Triggering __debug__

How __debug__ is raised and how to catch it.

python
# Triggering and catching __debug__
try:
    raise __debug__("example error")
except __debug__ as e:
    print(f"Caught __debug__: {e}")
    print(f"Type: {type(e).__name__}")

__debug__ is raised when true under normal execution; false when run with -o flag. Always catch specific exceptions rather than bare except clauses.

Handling __debug__

Basic error handling pattern for __debug__.

python
# Safe handling pattern
def safe_operation():
    try:
        raise __debug__("example error")
    except __debug__:
        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