Transcript from the "Try Except" Lesson
>> Nina: Try except, most languages have a concept of a try except block. Lets say that in my REPLI tried to make an integer out of the letter A. I'm gonna get a value error here. Python's like, I have no idea what to do with this, this is just bad input.
[00:00:25] If I don't want my program to quit, I notice in the REPL, exceptions get thrown, but we can continue working. In your Python file as soon as an uncaught exception is thrown, that ends your program. So a little bit of a difference there. This is a lot easier to use for testing, debugging, etc without completely losing all of your state.
[00:00:47] Okay, so let's say I get some user input.
>> Nina: If I want to catch this exception, I would put the code,
>> Nina: Under a try block and then I would accept and then I would pass in the name of the class of the specific type of error that I wanna catch.
[00:01:13] Luckily for us it's pretty easy Python just prints it out right here. So I'm gonna say except ValueError and print out, no way, that's not a number, what are you thinking, user?
>> Speaker 2: [LAUGH]
>> Nina: Okay, so what's going to happen here?
>> Nina: Our user input is A, we're gonna try to make it an integer, Python's gonna fail.
[00:01:45] What code is going to run?
>> Speaker 2: The code in the except print click.
>> Nina: The code in the except block. And just to show you again, if I have try here and I'm trying to make an int and I print out, I'm gonna say,
>> Nina: Sorry, I'm gonna quit that.
[00:02:07] And I'm gonna say my number is int of user_input. Print out, great number!, good job.
>> Nina: What's that?
>> Speaker 2: I said, you did it!
>> Nina: You did it, exactly.
>> Speaker 2: [LAUGH]
>> Nina: And now I'm going to accept on my ValueError, and say what are you doing? Okay, so it's gonna print out, No way!, that's not a number!
[00:02:37] But it's not gonna print out, great Number, why? Because we never got to this code. An exception was thrown here at this line and we immediately jumped to the except clause.
>> Nina: Your except clause can have multiple exceptions given as a tuple, parenthesized tuple. So, that code will run if any of those exceptions are thrown.
[00:03:13] So, let's say I have a dictionary, a simple dictionary and I have my user input. I can say try turning my user_input into an int, or if that works fine. And actually sorry, I'm gonna say that my user_input is the number 2. So just to be clear, our d has that in it, our user input is 2.
[00:03:55] So I'm gonna try to turn our user_input into a integer and I'm also gonna try to get the value in the dictionary on key based on the user_input. Now I can say except and pass in a list of or rather tuple of exception classes. So we know TypeError is gonna be been e one of them.
[00:04:22] What's the other one?
>> Speaker 2: Out ValueError.
>> Nina: Right, I'm sorry, ValueError. And what's the other one? What error do we get if we try to get a key from a dictionary that's not there?
>> Speaker 2: Shouldn't there be a column at the end of ValueError?
>> Speaker 3: KeyError.
>> Nina: KeyError, yeah.
>> Nina: Print, no,
>> Nina: Okay, so, I'm so sorry, let's see, let me try that again. So int(user_input) and I'm just gonna print it out.
>> Nina: And I also wanted to get the dictionary, not at user, but at user_input. And then the rest of this code will be the same except ValueError, KeyError, and then we're gonna print, no.
[00:05:18] So this time we will print the user_input. But then we're going to print, no,
>> Nina: Sorry, this prints out twice because of the REPL here. So this through right here and this general except clause caught both of them. We can also stack our try and excepts generally, we want to catch the more specific one first and the less specific one later.
[00:05:50] And one more thing about the except clause we can also give that exception a name if we want to do anything with it, look at the label, log it. So I can say, try make an integer out of this and then accept on that value error. And to give this a name I can say as e, or doesn't matter what you name it and then I can print out, no, as well as e.
[00:06:21] And that gives us the message for that exception.