BeginnerCoder

Lesson 4 — Exceptions

Handle failures

An exception is Python signalling that something went wrong, like dividing by zero. try/except catches it so your program keeps running. Name the error type after except, so only that problem is handled and other errors still surface.

try:
    age = int("twenty")
except ValueError:
    print("Please type a number")

Why it matters: user input and network calls fail all the time, and catching the right exception keeps the app usable.

Want it another way?
Task

Catch the ZeroDivisionError and print: Cannot divide by zero

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.