Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Assertions and Try/Except

In this lesson, you’ll learn how assertions are made and how to handle exceptions using try and except. You’ll see that assertions follow this general pattern:

Python
assert (condition), "Optional message if condition not met"
When assertions fail, they raise an AssertionErrorException. The second part of the lesson shows how to handle assertion exceptions when they come up using the try and except keywords. You’ll also see how to handle other types of exceptions and how to provide useful information to the user about the errors.

00:00 Next, you’ll look at the AssertionError exception. The assert statement checks to see if the condition in the brackets is True.

00:09 If it isn’t, then an AssertionError will be raised and the program will stop. Note that it’s possible to put an optional message after the assert statement which will be printed out alongside the AssertionError.

00:22 So, instead of waiting for a program to crash, you can start by making an assertion. You can assert that a particular condition is met. If it’s True, that’s fine and the program will carry on. But if it’s False, we can have the program raise an AssertionError and stop.

00:40 Here’s a program to demonstrate that. We’re going to set the value of name to be 'quaid'. Then we’re going to assert for the name is equal to 'quaid'.

00:59 Finally, let’s print something out to show that that’s what’s happened.

01:10 Let’s run the program. You can see, as expected, because the assertion is True it didn’t have any other effect on the program and line 3 gets carried out.

01:27 But what happens if the assertion is False? Let’s change the value of name to be something else. And now let’s see what happens.

01:41 You can see an AssertionError is generated because we asserted that the name was 'quaid' and it wasn’t, so we get an AssertionError.

01:50 And just like raise, we can clarify what’s happening by adding some texts after it which will be printed out when the assertion is generated. So here I’m going to add a string which will clarify what’s happened.

02:11 I’m running the program again. You can see the assertion is clearer, name is hauser, it should be quaid. Next, you’ll look at try and except and handling exceptions. Here you see the form. The try keyword is followed by a block of code, which is attempted to run. Next is an except statement.

02:36 This code is executed if an exception is generated. As we’ll see later on, it’s possible to specify particular exceptions or catch a general one, although it’s not typically a good idea to catch all exceptions as this may be masking a problem which will stop your program from running properly.

02:56 As you’ve just seen, the try and except block is used in Python to catch and handle exceptions. Python executes the code following the try statement as a normal part of the program and if an exception occurs, the program may be able to handle it depending on which exceptions have been caught. Let’s see that in action, starting out with a simple program.

03:31 x has been set to 1, the answer will be 1 divided by x, and then the answer will be printed. Here you’ll see the program running.

03:41 You can see The answer is 1.0. But what happens when you generate an exception? Well, let’s set x to be 0. This time, you can see that a ZeroDivisionError has been generated, and next you’ll see how to handle that.

04:02 The try block can be put here, and then indenting our statement where we’re trying to set the answer. Next, we’ll follow the exception.

04:19 In case of a ZeroDivisionError, the answer will be set

04:29 to 'undefined'. And running the program this time, the exception is caught and The answer is undefined. Now you’re going to see handling a different kind of exception if the input was a string. What happens this time?

04:54 We get a different kind of exception. We have a TypeError. So let’s handle that. Here you can see you can add multiple except blocks.

05:13 It’s fair to say that 1 divided by 'sausage' is 'meaningless'.

05:22 Let’s have a look at that, and you can see that that works. So by using the try and except blocks we can catch these exceptions.

05:32 And in this case, they give us some answers that do have some meaning but, most importantly, allow the program to carry on its business.

Become a Member to join the conversation.