Concept
What Are Exceptions: When Code Hits a Wall
An exception is an error event triggered at runtime (like dividing by zero). If unhandled, it immediately crashes your entire program.
python
# print(10 / 0) # ZeroDivisionError: division by zero
# print(int("x")) # ValueError: invalid literal
print("Without error handling, one mistake halts the server.")
# 108. What Are Exceptions: When Code Hits a Wall An exception is an error event triggered at runtime (like dividing by zero). If unhandled, it immediately crashes your entire program: ```python # print(10 / 0) # ZeroDivisionError: division by zero # print(int("x")) # ValueError: invalid literal print("Without error handling, one mistake halts the server.") ``` Syntax errors prevent code from even starting. Exceptions happen while your code is actively running, often due to unpredictable user input or missing files.
Checkpoint
What happens when Python encounters an unhandled exception?
Practice
Trigger a `ZeroDivisionError` on purpose in an expression and assign it to a variable, but comment out the line so the script prints `"Prevented crash"`.
01
Tap code blocks below to assemble
Available Blocks