 This is your program This is your program when it crashes and we don't like that Obviously when we're writing code because when things are on fire Our our code doesn't work So we never want our programs to crash especially when they should be operating like normal a great example of this Would be let's say for example I built a simple little function called divide takes an enumerator takes in a denominator and returns the You know quotient so the entire idea here is that well what happens if I attempt to divide? 10 By zero oh crap I tried to divide by zero and my program is going to error out and I don't want that all the time That is bad dividing by zero is bad Well in that case we can use exception handling to process our Data so that it's a little safer when we're running our code So the way to think about this is there are a number of different types of problems first one is something known as a syntax error and you were Doing syntax errors anytime you run your code and oh, I don't you know you misspelled a word or something That's actually what the typing exercises that you do for class are built to help out with But specifically we happen to have two other problems that we may need to work off of logic errors Not necessarily, but they can sometimes happen, but specifically we're looking at The runtime error if you think about it when I was working off of this code my program was running perfectly fine There are no like typographical errors here and technically this is not a logic error This is how you divide numbers So the issue specifically is that by passing in that zero parameter Again, I'm attempting to divide by zero laws of math say we are not capable of doing that And so as a result Python is not going to try and break the laws of math. It's just going to crash So with that in mind again, that's where we get into the idea of exception handling and that idea of an exception What we're thinking about here is that when our code is running there may be some Issue that causes the code to crash and that would shut down our program But maybe we don't want our code to crash And in that case just as the you know slides sort of describe it as we want to handle that exception, so how do we do that? We use some more keywords specifically we add in something called our try and our accept Now try is effectively going to say just as you can probably imagine try To do this Whatever that happens to be if it were to crash so if that crashes do this That's in a nutshell how exception handling comes into play So if we were to come in and for our sake Let's attempt to try To divide by zero again, you know, let's just try to do it Except now I'm going to start with just accept Accept is going to be a catch all whatever crash happens. Just that crash is going to happen. So print You cannot divide by zero so we've now built that out and Now if we attempt to run our you know divide by zero Instead of a crash happens. We get a print statement that says you cannot divide by zero crisis averted Now the issue is that what happens if instead of trying to divide by zero I try to divide by a String I try and divide by cat. Well, that's not a number and in fact the issue is that We're still getting the same error message We're still getting say for example explicitly the catch all of you can't divide by zero So let's take off The try catch for just a moment and now see just what error is going on there Type error. Oh specifically notice again that these are different types that Python can Present to us and there are a number of these Not every one of them is going to happen all the time some are more rare So it depends on really your code, but Specifically to our sort of example here. We are experiencing a zero division error because we're trying to divide by zero and a type error where we're using an inappropriate data type So in this case for example This is where we can use different types of Exceptions or handle different types of exceptions. So once again, we'll reintroduce the exception handling but instead of just saying Except we're going to add in a space and then denote that I want a specific type of exception to be handled this way zero Division error only a zero division error should be handled this way. So in this case Zero division Did I misspell it? No, sorry So now zero division errors will print out the you can't divide by zero However, if I try and divide by a cat, I still get the type error This is one of the things where depending on how your code operates. You're actually able to nest multiple exceptions Just like if we were dealing with Conditionals and then using L ifs. So if you think about it, there was the if statement and then L if New conditional statement L if a new conditional statement L if a new conditional statement Sort of the same things going on here Try to do the code if there's an error if that error is a zero division error Do this L if it's a in this case not a zero division, but if it is a type error Do this and so in this case we'll say I you Cannot divide strings Okay, so in this case once again, we're handling two separate exceptions We've loaded that in the memory now if we attempt to divide by a cat Instead of seeing you cannot divide by zero we Instead caught our type exception and outputted you cannot divide by strings