About Lesson
Raising Exceptions
- Definition: The
raise
statement allows you to trigger an exception if a certain condition occurs. - Syntax: python
raise ExceptionType("Error message")
- Example: python
def check_age(age): if age < 18: raise ValueError("Age must be at least 18") try: check_age(16) except ValueError as e: print(e) # Output: Age must be at least 18