We'll learn about exceptions and how they work, as well as how we can handle them gracefully.

<aside> 📌 Note that "exception" and "error" will be used almost interchangeably in this course.

</aside>

Exceptions - something has gone terribly wrong!

What is an exception? To a computer, an exception is a place where you've done something so terrible that the computer gets confused and your code stops running. There are many types of exception in Python, but a lot of the time they show that something has gone wrong.

If an exception is raised i.e. occurs, your code will stop running.

There are many forms of exception, but a few common ones are listed below.

SyntaxError

You may have come across a SyntaxError already! Syntax refers to the structure of both English prose and code, so a SyntaxError suggests that you made a mistake in the structure of your code.

Unlike in English, programming languages follow a rigid set structure. By not following this structure, your code doesn't make sense - for instance, print( makes no sense without a closing parenthesis, because the computer doesn't know how many parameters you want to pass in. As a result, it creates a SyntaxError.

SyntaxErrors occur before your code starts running, during the 'compilation' phase. The extension section has an explanation of what compilation is.

TypeError

When we're trying to do things to an object of an inappropriate type, a TypeError is raised. Example: