← Courses

Python for AI — Beginner Foundations

Data Science

Error Handling · Lesson 50 of 76

Try and except

The full shape of error handling has up to four parts:

Python
  • try — the risky code.
  • except — runs only if that specific error happens.
  • else — runs only if there was no error.
  • finally — runs every time, error or not (good for cleanup).

You'll mostly use try/except; the other two are there when you need them.

Your turn: call safe_divide(20, 4) and read which branches print.

You’re reading for free. Sign in to keep your progress and earn a certificate when you finish.Sign in to keep my progress →