20
Lesson 31 of 44

Error and Exception Handling

Learn how to handle runtime errors using try, except, finally, and create custom exceptions.

  • 8 theory articles
  • 12 practice exercises
Start lesson

Fundamentals concepts 8

What is an exception in Python?

An exception is an event that interrupts the normal flow of a program. Python raises exceptions when a runtime error occurs, such…

Theory
Read more

The try-except structure

In Python, the try-except structure is used to handle errors. The try block contains code that might raise an exception. If an…

Theory
Read more

else, finally and the complete structure

You already know try and except . The complete structure has two more blocks, else and finally , which give you control over what…

Theory
Read more

Exception types and how to read an error

Each Python error is of a different type, and telling them apart lets you react differently to each one.

Theory
Read more

Raising exceptions and creating your own

Until now Python raised the exceptions. You can raise them too, to warn that something is not valid, and create your own types…

Theory
Read more

Best practices and common mistakes

Knowing the syntax is not enough: there are good and bad ways to handle errors. These are the ones worth adopting from the start.

Theory
Read more

Chaining exceptions and adding notes

When one layer of your program catches an error from another and raises its own, the information about the original cause must…

Theory
Read more

Exception groups and except*

Usually a program stops at the first error. But sometimes —validating lots of data, running tasks in parallel— you want to…

Theory
Read more

Practice exercises 12

Zero division capture

Write a program that asks the user for two numbers and tries to divide the first by the second. If an error occurs (e.g…

Exercise Medium
Solve it

Handling multiple exceptions

Write a program that asks for a string and a number. Try to convert the string to an integer and divide it by the number. Catch…

Exercise Medium
Solve it

Safe integer

Create a Python program that reads a text and converts it into an integer without the program crashing if it is not one.

Exercise Easy
Solve it

Safe index

Create a Python program that shows an element of a fixed list from the position typed by the user, handling positions that do not…

Exercise Easy
Solve it

Retry until valid

Create a Python program that asks for an integer between 1 and 10 and keeps asking until the user types a valid one.

Exercise Medium
Solve it

Validated square root

Create a Python program with a function that calculates the square root and raises an exception when the number is negative.

Exercise Medium
Solve it

Custom exception

Create a Python program with a custom exception to control withdrawals of money from an account.

Exercise Medium
Solve it

Calculator with finally

Create a Python program that divides two numbers and always shows a final message, whether or not there is an error.

Exercise Medium
Solve it

Chained errors

When you catch a low-level error and raise your own, you should chain them with raise ... from e : the new error explains what…

Exercise Medium
Solve it

The order of try, else and finally

Understanding the order in which try , except , else and finally run is key: finally always runs, even when the try has already…

Exercise Medium
Solve it

Exception groups

Since Python 3.11, ExceptionGroup groups several errors into one and except* lets you handle each type separately. It is useful…

Exercise Hard
Solve it

Retries with a custom error

A custom error can carry data (attributes) besides the message, so whoever catches it can decide what to do. Here we simulate…

Exercise Hard
Solve it