Search⌘ K
AI Features

Catching the Exceptions

Explore how to handle multiple exceptions correctly in Python 3 by using the recommended syntax. Learn to bind exception instances properly and avoid deprecated methods from Python 2, improving your exception handling skills.

We'll cover the following...

Let’s see if you have been practicing your catching correctly.

⚠️ The following code is meant for Python 2.x versions.

Python
some_list = [1, 2, 3]
try:
# This should raise an ``IndexError``
print(some_list[4])
except IndexError, ValueError:
print("Caught!")
try:
# This should raise a ``ValueError``
some_list.remove(4)
except IndexError, ValueError:
print("Caught again!")
...