From the course: Python Essential Training

Errors and exceptions - Python Tutorial

From the course: Python Essential Training

Errors and exceptions

- [Instructor] We've seen a lot of errors working with Python so far. You know, it's that thing that happens when you divide something by zero. You get a zero division error. Working with Python, you'll see that sometimes these things are called errors and sometimes they're called exceptions. And if you read the official Python documentation, you'll find something like exceptions are decided during runtime and are retryable, errors are not retryable, eh. There are so many, well, exceptions to this rule even within the official Python code it's not really worth worrying too much about. They all work the same anyway. Controversial opinion, but errors and exceptions are the same thing. All Python errors and exceptions ultimately extend a class called the base exception. So division by zero error extends arithmetic error which extends the exception which extends base exception. And base exception is the thing that gives us really useful and powerful properties of exceptions, which is to halt the execution of code and give you some information about how and why that execution was halted. In this zero division error, for example, we can see the file this occurred in. It's a little bit difficult to tell because it's all a Jupyter Notebook file, but we can also see the line, line one. And if we put 1/0 into function, let's make it called causeError, and then this is going to return 1/0, oh, and of course we need to call it causeError. We can see that this gets a little bit longer. We can see where the function was called originally, then we can see where in that function that we called the error happened. And this is called a stack trace. This whole thing is called a stack trace. If you think about these nested calls like a stack of operations, it provides a trace through the stack that we can use to debug our program. And we can make the stack trace even larger by doing something like this. Let's make a function called callCauseError, return causeError, and of course, we're going to call that one. Now our stack trace gets even larger. You can imagine that in very large programs with many files, these stack traces can get fairly large, so it's important to be able to read them and also write your code clearly and pay attention to your program's architecture so that it doesn't get too difficult to debug. Exceptions might seem intimidating at first. After all, any tiny mistake in any of hundreds of lines of code could cause your beautiful program to just blow up, right? They're terrifying! But don't worry. They're actually just classes. We'll cover this in more detail later, but we can catch an exception using a try except statement, and we can get an instance of that exception that was raised. So, let's give it a try. Let's just do 1/0 in here and then except. Going to except exception as e, so e is going to be our variable. Actually, that instance of the exception that was raised, print type e, and there you go. This thing is a zero division error. We have caught this exception. We have it, it's not being raised anymore. It's just a class, it has attributes, you can create them, they can even be returned. Exceptions are nothing to worry about, but they do require some careful consideration. Exceptions when used and handled correctly are almost like a secondary layer of code underlying all the existing and more obvious code. So stick with me and we'll learn how to write code that if not error free, at least has beautiful errors.

Contents