ExceptionEasy Examples

Base class for all non-exit exceptions; used to define custom exceptions

Triggering Exception

How Exception is raised and how to catch it.

python
# Triggering and catching Exception
try:
    raise Exception("generic exception")
except Exception as e:
    print(f"Caught Exception: {e}")
    print(f"Type: {type(e).__name__}")

Exception is raised when base class for all non-exit exceptions; used to define custom exceptions. Always catch specific exceptions rather than bare except clauses.

Handling Exception

Basic error handling pattern for Exception.

python
# Safe handling pattern
def safe_operation():
    try:
        raise Exception("generic exception")
    except Exception:
        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