Loading video player…

Exceptions and How to Raise Them

This lesson introduces exceptions in Python. You’ll learn of the difference between syntax errors and exceptions. After a few examples, you’ll see how to raise exceptions when certain conditions are met using the raise keyword

00:02 Hello! I’m Darren from Real Python and welcome to this video where you’re going to take a look at an introduction to Python exceptions. You’re going to see the difference between exceptions and syntax errors, how to raise an exception, the AssertionError exception, how to handle exceptions with try and except, the else clause, and using finally. First, you’re going to take a look at the difference between exceptions and syntax errors. Here, you can see a syntax error. You can see at the end of the print statement there are two brackets where there should only be one.

00:39 If you try and run this program, the parser detects the incorrect statement, doesn’t run the program, and highlights the incorrect syntax to you. On the right of the screen, you can see an exception has happened.

00:54 You can see that the code is correct—we don’t have the extra bracket—but it’s trying to divide by zero. And if you run this program, you can see it generates a ZeroDivisionError. Exceptions are raised when code with correct syntax results in an error.

01:13 Let’s see that in action. I’m going to be using bpython, which is a color-coded version of the Python interpreter, but if you enter these commands into the standard Python interpreter by typing python, you’ll get the same result. Firstly, you’re going to see generating a syntax error.

01:35 There you can see you’ve got a syntax error, it says invalid syntax, and it’s helpfully pointing to the position where the error is. It’s showing us that we’ve got an extra bracket on the end of our statement.

01:47 So that line doesn’t get executed. Now you’ll see generating an exception.

01:58 And there you can see by trying to divide by zero, a ZeroDivisionError has been generated. Next, you’ll look at raising an exception. You can use the raise keyword to generate an exception when a condition occurs.

02:16 Next, you’ll see a short program which will illustrate that in action.

02:37 So here on line 1 x is set to 5 and on line 2 is compared with the value of 5. If it’s greater than, an exception will be raised. And on line 4 there’s a print statement to show we got to the end of the program. Now at the moment if we run this, we shouldn’t see an exception because the condition on line 2 isn’t True.

03:03 And you can see that it made it to the end. Now, let’s change the value of x and see what the difference is. With x set to 10, we should get an exception.

03:19 And there it is! We just have a plain Exception. You can also add some text at the end to help clarify why the exception was raised.

03:40 Here, the string will print out the value of x that was passed to it, and if we run the program once more, we can see now our exception has some informative text in there.

Avatar image for Peter T

Peter T on March 31, 2019

Great class, albeit, very short, even though it seemed to cover all of the features of Exceptions in an extremely clear & succinct way. I look forward to seeing more courses by Darren.

Become a Member to join the conversation.