Courses/Errors & Files

⚠️ Errors & Files

Raising Exceptions

Throw your own errors with clear messages.

Use raise to signal an error. Pick a built-in class (ValueError, TypeError) or define your own.

def sqrt(x):
    if x < 0:
        raise ValueError("x must be non-negative")
    return x ** 0.5
main.py
Output
Press Run to execute.
Expected output
Error: age cannot be negative

Sign in to track your progress across lessons.