Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Recovering From Errors

00:00 Now you’re going to look at another aspect of control flow, which is errors and recovering from them. But before you can recover from errors, you’re going to need to learn what errors and exceptions are. And to do this, you’re going to look at different types of errors and exceptions.

00:19 First, take a look at a SyntaxError. Most of the other errors you’ll encounter are actually exceptions, which means that they can be recovered from. However, a SyntaxError is a proper error in the sense that it can’t be recovered from. If you have a SyntaxError, your program just won’t run, no matter what you do.

00:38 An example of a SyntaxError is using if without a condition or a colon. As you can see, if you try and execute just a bare if, you’ll get a SyntaxError, and it will tell you that the syntax is invalid.

00:52 And again, you can’t recover from this kind of error. But in the following examples, you’ll see errors that raise exceptions, which can be handled gracefully.

01:03 An example of something like this is a ValueError. In this example, you’re using the int() constructor—int short for integer.

01:12 And the constructor, you can tell it’s a constructor by the brackets (()) here, as if it’s a function. But instead of passing in something that resembles a number, you’re passing in "hello", and int() can’t understand this. It says, well, "hello" is not even close to a number, so it raises a ValueError saying that it’s an invalid literal for int() with base 10. It’s not a number, basically. So this is a ValueError, which raises an exception that can be caught. More on what “can be caught” means in the following lesson.

01:45 A TypeError you might have run into already. If you try and add a string to a number, as is the case in this example—it’s adding the string "1", so it’s one enclosed in quotation marks, plus 2, a number that’s not enclosed in quotation marks—and they’re saying there’s a TypeError here because their types are wrong here, and it’s saying, you can only concatenate a string to a string, not an integer to a string. A NameError.

02:16 So say you try to print a variable, does_not_exist. You haven’t declared this variable, does_not_exist, and so you’ll get a NameError with a message saying that the variable 'does_not_exist' is not defined.

02:32 There’s also ZeroDivisionError. So if you try and divide something by 0, which is not possible to do, you’ll get an error, descriptively called the ZeroDivisionError.

02:44 Then you’ll have an OverflowError. In this operation, you’re using the pow() function, which tries to raise 2.0 to the power of 1_000_000.

02:55 Now that’s a mind-bogglingly large number, which Python will protect you from and will give you an OverflowError. Now again, all of these errors, apart from the SyntaxError, are ones that you can handle, and that will be covered in the next lesson.

03:12 In this lesson, you’ve covered errors and exceptions, the differences between them, and you’ve looked at different types of errors and exceptions. You’ve seen how errors and exceptions have quite descriptive names and helpful messages to help you debug them.

03:27 There are many more errors and exceptions that you haven’t covered here. And in fact, you can even define your own errors and exceptions with custom names and messages. That’s just for some context, though. Don’t worry about it yet, as you don’t need custom errors and exceptions while learning the basics.

Become a Member to join the conversation.