Raising an Exception

Learn how to raise exceptions in Python.

We'll cover the following


We’ll get to responding to such exceptions in a minute, but first, let’s discover what we should do if we’re writing a program that needs to inform the user or a calling function that the inputs are invalid. We can use the exact same mechanism that Python uses. Here’s a simple class that adds items to a list only if they are even-numbered integers:

Get hands-on with 1200+ tech skills courses.