Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Exploring Exceptions

00:00 In this lesson, you’re going to look at some exceptions. You’ve learned that a return code of zero means the process ran without error, and that any other code suggests a process error.

00:14 So which errors can we expect to see and how does subprocess deal with them? Well, let’s have a look. So if you please move to the folder where your code lives and then open a REPL with Python or Python 3.

00:29 So firstly, let me show you again how the code looks without error. So this is the code you’ve seen before. If we press Enter, we run it. The timer starts for five seconds,

00:41 five dots, the word Done, and then we have the CompletedProcess here, which reflects the CompletedProcess object that the subprocess.run() function returns, and we have the return code equals zero.

00:56 So no errors. So now let’s break some things. If we go here and you press up, that brings back the last line. And if instead of Python, I’m going to run an app that doesn’t exist, something called pyth, what does that do?

01:12 You press Enter, and then you get a FileNotFoundError. So you get traceback here and you get all the way to the end and it says FileNotFoundError.

01:23 So there is no such file or directory called pyth. And given that the process didn’t actually run, there is also no CompletedProcess object. So far, that behaves as expected.

01:37 I would say we run something that doesn’t exist, therefore we get an error. But if you press up again and you correct this to Python, but create another error, and then run a Python file that doesn’t exist.

01:51 So if you do simple_timer instead of simple_ timer.py, what does that do? We expect an error. Do we get an error? Well, kind of.

02:03 We get a warning that says Python can’t open the file. So okay, something went wrong clearly, but subprocess.run() does return a CompletedProcess object.

02:15 So the process has completed, it just shows a returncode=2.

02:22 Now if you run this from the REPL and you read the output, you might go, oh right, something went wrong. But if you run this as part of a bigger process, this doesn’t actually tell you something went wrong because it doesn’t return an error.

02:37 Previously it returned an error, so up here we had a FileNotFoundError. In this case, we do not have an error. So depending on the purpose of your code, that is unwanted behavior. How do we fix that?

02:51 Press the up arrow and we leave the error as it is. So we don’t have the .py, so we do expect it to go wrong. But after the list of arguments, type comma and then add the check argument and set that to True.

03:06 If you run it now, you will see a lot of text and where did it start? So we start from here, check=True, and then you do get your traceback and at the end it says raise CalledProcessError.

03:20 So that is better, as in that is probably more what you would expect. You actually do get an error. By adding check=True, an error is raised as expected.

03:33 In the next lesson, you’ll find out more about the TimeoutExpired error.

Become a Member to join the conversation.