Back
+10 XP
Concept

The `try / except` Block

Wrap hazardous code in `try:`. If an error occurs, Python jumps to `except:` instead of exploding.

python
try:
    result = 10 / 0
except:
    print("Caught error! Program keeps running.")

# 109. The `try / except` Block Wrap hazardous code in `try:`. If an error occurs, Python jumps to `except:` instead of exploding: ```python try: result = 10 / 0 except: print("Caught error! Program keeps running.") ``` Without `try/except`, a single malformed request will crash your entire web backend. Catching exceptions lets your software fail gracefully and stay online.

Checkpoint

What happens to the code inside `try:` after an exception occurs?

Practice

Wrap `val = int("not_a_number")` inside a `try / except` block. In the `except` block, print `"Handled error"`.

Terminal
01
Tap code blocks below to assemble
Available Blocks