Exceptions are commonly used for error reporting in Python. Python uses the try and except statements for catching exception errors.
- [Instructor] Exceptions are a powerful runtime error reporting mechanism commonly used in object-oriented systems. Here in Komodo, I've opened a working copy of hello.py from Chapter 10 of the exercise files, and what I'd like to do here is just generate an error. We'll say "x = int of" and we'll give it a string, and so that's an error, and when I run this, you see we get this error message down here. Just expand this a little bit, and there's a lot of stuff here.
At the very bottom, you'll see it says "ValueError: invalid literal for int() with base 10: 'foo.'" That's our actual error message. Everything else is what's called traceback, and you can see it says "Traceback," and it goes bottom to top. The actual line with the error is on line 5, and you see that my cursor appears on line 5, and that's where the error is. Line 7 is where that is called from, and so down here on line 7 is the main call, which calls the function that has the error.
This traceback, under some circumstances, could help you to trace where the error is or what caused the error, but for our purposes, we're looking at the error itself, the exception error, and you see that it says "ValueError." ValueError is a token. It's the name of the type of an error that is being generated here. What I can do is I can catch that by catching the exception. You do this with the try command, the try statement, rather, and you can put whatever you want in the try block.
Get my cursor out of here, and then I say "accept," and this is what will be executed if the try fails, if it generates an exception, and I can catch a specific section by saying ValueError, and you notice there that I get a list of the possible exceptions, and there's a lot of them, but we know this is ValueError because we see it in the error message down there, and so what am I going to do if I catch a ValueError? I'm going to say "print I caught a ValueError," and I save and run this, and now, instead of getting that whole error message, I have captured the error.
The other nice thing about this is that by capturing the error, I can handle it intelligently, and I can go on with my code. I can actually do something else because I know that this didn't work. I could assign a rational value to x, or I could call a function or whatever, but I've caught that error and now my execution continues. If I don't catch the error, then it actually stops the execution of my script, so there's a number of different possible errors here.
I can say "x = 5/0." I'm dividing by zero, which is generally going to cause an error, so I have a zero division error. I can actually capture several different types of errors in the same try block. "Zero division error: print don't divide by zero," and you notice that I have this single quote there. I can escape that with a backslash, and now when I run it, it'll say "don't divide by zero." I can do this as many times as I want to all in the same try block to catch different types of errors, and if it succeeds, I can use "else," and this will only be executed if I don't have an error.
Print. Good job, and I can divide this by three instead of by zero, and I can actually print the value here too if I want to, and now it says "good job" and there is five divided by three, but let's say that I have an error that I wasn't expecting. Maybe I'm not expecting the zero division error. I can have a default except that does not know which error it is, and I can say "unknown error," and I did that, okay.
Now, if I'm dividing by zero, I get this unknown error, but I can still continue because I caught the exception. If I want to know more about the error, I can import sys, which has a lot of constants in it that'll help me to understand things like this. I can make this an f string, and I can say "sys. exec_ info," and that'll give me more information about the error, and I'll run it. Yeah, I misspelled it.
Like that. Now, it tells me more about the error, or, if I want to, I can actually subscript from that and get the specific error. Save and run, and it says "unknown error: division by zero." Python has a rich and complete system for handling exceptions. You can also generate your own exceptions, and we'll look into how to do that in the next lesson.
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api