Raising and Handling Python Exceptions (Overview)

A Python program terminates as soon as it encounters an error. In Python, an error can be a syntax error or an exception.

In this course, you’ll learn what an exception is and how it differs from a syntax error. After that, you’ll learn about raising exceptions and making assertions. Then, you’ll learn how to catch exceptions to prevent your program from unintentionally ending and to change the control flow of your program:

Diagram explaining try except else finally statements

You’ll learn about the basic use of a tryexcept block, as well as how to extend it using else and finally.

In this course, you’ll learn how to:

  • Differentiate exceptions from syntax errors
  • Use the assert keyword to check if a certain condition is met
  • Write custom exceptions that subclass Exception
  • Catch exceptions with a tryexcept block
  • Use the additional keywords else and finally
  • Catch built-in exceptions

Sample Code (.zip)

1.7 KB

Course Slides (.pdf)

1.5 MB

00:00 Hello, and welcome to this Intro to Exceptions course on Real Python. Because there’s always something that can go wrong when you’re experimenting while you’re writing code, it’s good if you have some tools to handle those exceptions that might occur.

00:15 In this course, you’re going to walk over all the different keywords that are related to exceptions in Python, and you will learn how to use each of them at the hand of building out a little program.

00:26 You’ll start off by differentiating exceptions from syntax errors, so finding out what causes a syntax error and what is an exception. Then you will learn how to raise an exception yourself using the raise keyword.

00:39 You will learn how to use the assert keyword to check for a true condition and then raise an exception if the true condition doesn’t pass. Then you will learn the more familiar way of catching exceptions in Python, which is using the tryexcept block that you’ve seen around in other Python code, and you’ll learn how to use that.

00:58 You will learn about the lesser-known part of this tryexcept block, where you can follow it up with an else clause that only executes if no exception was raised.

01:08 Then you will also get to know the built-in exceptions in Python. I’ll show you a list, and we’ll integrate it into the little program that you’ll build throughout this course. And then, finally, you will also get to know the finally keyword, which, again, can be part of the tryexcept block and can include a bit of code that’s going to run no matter whether or not an exception was raised during the tryexcept block.

01:33 That’s going to be it, and then there’s a little summary at the end like always in our courses. And by the end of it, you will know all these exception-related keywords in Python.

01:44 You’re going to have a bit of practice using them by building them out in a small program, and you will be better equipped to handle any sort of problems that might arise in your code and to write better Python code. So in the next lesson, you’ll get started by learning the difference between what’s a syntax error and what’s an exception.

Become a Member to join the conversation.