Transcript from the "Exception Types" Lesson
>> Nina Zakharenko: Let's talk about exceptions. We've seen exceptions quite a bit over the course of this class. We've seen type error, syntax error, value error, indentation error. But we haven't really figured out what to do about them. We saw some simple try excepts yesterday. But that's not enough in the course of a more complex program.
[00:00:24] So I'm gonna tell you a little bit about the exception of hierarchy, about raising exceptions, and about how do you use try catch, and how to create our own custom exception process. So a little bit about exceptions.
>> Nina Zakharenko: The built-in exceptions and exception handling, it's a really nice feature of Python.
[00:00:54] Those syntax errors that we've seen, they do a pretty good job of telling us what went wrong, right?
>> Nina Zakharenko: If we get an indentation error, we know that we didn't indent our code properly. If we get a value error, that means that something is wrong with the input that we gave, right?
>> Nina Zakharenko: Even if the syntax is totally correct, sometimes errors occur when your program is run. For example, if you tried to run our final exercise from introduction to Python and you didn't have a valid internet connection, it would just raise an exception that we didn't catch and exit the program.
[00:01:35] So even if your syntax is 100% correct, your program can still have exceptions. Unhandled exceptions are fatal to your program. It's gonna print out a traceback and then quit your code wherever it was at that time.
>> Nina Zakharenko: And it'll stop your interpreter. It'll exit your program. So for production code, having uncaught exceptions is pretty bad, right?
[00:02:02] Just take down your website, depending on kind of what you're seeing. Or if you're using a web server, your end user will see a 500 error. And that's not very good, right? But once you learn a little bit more about these exceptions and about how to handle them, it'll help you write apps that are a lot more robust in the face of issues.
[00:02:29] So some common types of exceptions that you might see, there are a few more in the Python documentation, but very quickly. AttributeError, if I tried to use a attribute name that wasn't on my instance, I would get this error. ImportError, if I try to import something that wasn't found.
[00:02:49] KeyError, we've seen this one, if a key was not in our dictionary. SyntaxError, the parser just doesn't even know what to do with your file. IndentationError, if you're messing with the level of indentation for if statements, classes, etc, functions, where you need to have your first line of code indented one level.
[00:03:13] And this KeyboardInterrupt error, that's an interesting one. So for those of you who have been seeing me use use the REPL,
>> Nina Zakharenko: You've seen me hit maybe Ctrl+C. If I'm typing something and I'm like, I made a mistake, I don't wanna hit Enter and get a syntax error.
[00:03:39] I know that this isn't valid code. I could hit Ctrl+C. It doesn't run my code. And in this case, it generates a KeyboardInterrupt error.
>> Nina Zakharenko: For programs that you run on the command line, if it's taking a really long time, we've been writing pretty fast scripts, and you wanna exist out of it, you can hit Ctrl+C.
[00:04:04] It'll generate a keyboard error, and it'll quit your program while it's running.
>> Nina Zakharenko: Just a quick thing about this KeyboardInterrupt error. Let me take a look at the Python documentation here. So these built-in exceptions, we'll see that there's an exception base class. And all of the exceptions mostly use this base class in terms of their object hierarchy.
[00:04:35] We, in our program code, don't want to do that. If we're going to be handling exceptions, we always want to handle types that are of exception Exception, never BaseException. Exception here is one of the subclasses of BaseException. If we catch BaseException, we're gonna also catch those KeyboardInterrupt errors.
[00:05:04] Someone will be running your script, press Ctrl+C, and it won't do anything. And they're gonna get pretty mad at you. So remember that, don't catch BaseException, catch Exception instead.