Welcome back in this lecture we're going to be discussing errors and exception handling errors are bound
to happen in your code.
Nobody's perfect and errors are definitely going to arise especially when someone else ends up using
your code in an unexpected way.
We can use error handling in an attempt to plan for possible errors that may arise in our code in the
future.
For example maybe in some part of your code you try to open up the file so later on someone else is
using your code and the user may try to actually write to the file when it was actually only opened
with the mode are for reading currently if there's any sort of error inside our programs the entire
scripts are going to stop and we get back some sort of error statement.
We can actually use error handling to let script continue if the other code and report the error.
So even if there's an error the code will report the error and continue on.
Now there's three key words for this process of exception handling.
And it's the try except.
And finally keywords and these are all going to have a block of code associated with them.
So under the try block of code that's the block of code that's going to be attempted and this may or
may not lead to an error.
So once you try the actual try block of code if there is an error you have the accept block and this
is a block code that will execute in case there's any error inside of that try block.
Then you have a finally block of code which is a final block of code which is always executed whether
or not there is an exception or error occurring.
Let's jump to the notebook and see some examples how to use these blocks.
OK.
Let's start off actually viewing an example of some error that may occur.
Let's imagine that we have a very simple function some add function takes in and one and two and then
what it does it just prints out the sum of them and one plus and two you may be thinking no problem
here.
I'm just going to say add 10 and 20 and you get back 30.
But let's imagine you have the following situation.
You have some variable number one.
You set that equal to 10.
Then you have a bunch of code and later on to get the second number you ask for it as an input and let's
say number two is equal to the input of.
Please.
Provide a number.
So it asks us please provide a number.
We say 20 and then what we end up doing is we say well let me call the function again on number 1 and
number two.
Hopefully you realize that this is already going to give you an error because notice recall that when
you call the input function 20 here is actually a string.
So when I run this it says hey you have a type error.
So notice here that there is an actual specific exception that Python is trying to tell you about type
error and that says unsupported operation type as four plus integer and string.
So basically telling you hey I can't add an integer and a string together I can add two integers together
or I can concatenate two strings together but I can't work with both these data types.
And this is an example of the type of error you may get while running your code.
And unfortunately let's say I wanted to have a print statement.
So let's say something happened.
And I run this this print statement is never actually going to occur.
What's going to happen is I get an error.
And that's the end of my script.
Doesn't matter if I have hundreds of lines pass this block of code.
I will still get this type error and nothing else after it is going to get executed.
The idea behind the try and accept and finally statements is that even if it ever happens we're going
to try to do that block of code.
And even if there's an error we can continue with some more code.
So how will we actually do this.
Well we have to try keyword colon.
And then we have the block of code.
So this is the code that you want to try.
So you want to attempt this code however it may have an error.
So here I'm going to say result is equal to 10 plus 10 whoops ago.
So we know this is not going to actually induce an error.
And then what I'm going to do here is I'm going to have my except block of code.
So my except block of code is what I want to have happen if there is an error and this is really common.
When you're building out larger libraries that other people are going to use you can have except block
so you can print out a more specific message to the user.
So maybe you can say hey it looks like you aren't adding correctly.
So I'm going to run this and this.
We actually don't induce air.
Instead I can call results here with no problem as 20 which makes sense because Templeton has no problems.
Let's now actually create that air.
I'm going to try to add the integer 10 to the string 10 here.
Now when I run this it says hey it looks like you aren't adding correctly.
Now notice what happened here.
The rest of the block of code was still able to execute with the accept statement my whole program wasn't
shut down just because I had some sort of error like before.
Instead I tried this block of code and then I said except and in general here I can say except if there's
any type of error do this code right here.
Now let's imagine that I want a block of code to execute if there is no exception.
I could do something like this.
I can say.
Else Prince results and above that I can say Prince ad went well.
So right now when I run this it just says hey it looks like you aren't coding correctly.
So this is known as a try.
Except.
Else statement.
And what we have here is the block of code we're going to try.
And then we have two options.
If there is an error in that try block we run the except code else.
If there is no error there we run this.
Else code.
So let's fix this and have it not produce an error anymore.
We run this.
We try this block of code since it was successful.
We don't execute this except block of code.
Instead we jump straight to else and we say ad went well and we have 20.
OK.
So let's try.
Except.
Else we can now actually use the finally key word to have a block of code.
Always executing.
Regardless if there's an error.
So the show this I'm going to build off of an example where we actually try to write some files.
So let's explore this concept of try except.
And finally by working on a new example and in this example we're going to try to open up a file so
we'll have some test file and we're going to start by opening it in W mode.
That's right mode which allows us to either open the file and write to it or create the file if it doesn't
already exist.
And then I will say F that right and I'm going to write a test line to this file and then I will say
except and here I can either say except colon and that's going to Except for any errors.
But there's actually many types of errors I can specifically except for for example I can try to only
except for this type error.
So what happens is noticeable.
We had this specific type error.
What I could do is say hey only if there is a type error print out.
There was a type error.
Then I could try another error that's actually associated with opening and writing files.
And if you go to the documentation you can come to errors and exceptions and this explains syntax errors
and exceptions in general.
But you'll notice there's a link here it's a built in exceptions which jumps to Section 5 here of the
documentation and this is actually a list of all the different types of exceptions and errors that may
happen.
So if you keep calling down there is an assertion error there's and the file error import errors index
errors.
Some of these you may have seen before a lot of what we haven't seen there is OS errors recursion errors
overflows all different kinds of errors.
You can specifically try to catch.
So in our case the error that's going to occur when you're opening and writing to files you don't have
permissions to is an OS.
Err.
And we're going to print pay you have an OS air you can also say I own air depending on what version
of python you have and then we're going to add in a finally block and this finally block is always going
to execute no matter what.
So say I always run going to run this right now and right now we just get back.
I always run.
So what's actually happening here.
Well we open up the test file we call right on it so that all works meaning I don't have this except
block running and I don't have this except block running.
So then I just say finally I print.
I always run.
Now it's actually induce an error by opening it with our So that's read only permission.
So we should get an error on this line when we try to write to the file.
Now we shouldn't get a type error because that's not actually the error we're getting.
Instead we should get an OS error because that's the error that's related to trying to write to a file
that you only have read permissions for.
So if I run this I get back.
Hey you have an OS error.
And unlike last time where we had this except else we're only one of these blocks is going to run with
the finally statement the except block will run and then the finally block will run.
And this is how you can line up a bunch of excepts to have specific things print's for specific errors
where you could do is let's say that you have type error here and you didn't want to memorize all the
other types if you just say except at the end of this.
This is going to be all other exceptions.
So all other exceptions and every run again.
And it says all other exceptions because it didn't detect the type error or any other errors that we
defined.
So if you just say except colon you'll print out that error.
OK.
So keep in mind as you're beginning in Python you probably won't memorize all the error types and most
likely your blocks of code are just going to look like this.
Try.
Except.
And finally.
So just to reiterate the try block of code is the block of code you're going to try to attempt except
block of code is going to occur when there is an error in your try statement.
And then you have the option of putting a finally block here that will always run whether or not there
is an error or you can have an ELSE block code that's only going to X-2 when there's not an error.
OK so let's focus on this one more time here.
Right now there is no there is going to be no error because they will say they'll be there.
If I run this I get back.
I always run.
So there's no error.
Finally block still runs.
We induce an error.
We say all other exceptions because I'm just saying and generally except here it does whatever.
This block of code says finally it does this block of code.
So this finally block regardless if there's an error it's always going to run.
So let's finish off our discussion here by showing you how to use the tricks that finally blocks inside
of a function that tries to get a specific type of input from a user.
So we're going to do here is the following.
We'll say DPF ask for I a..
So what this function does is it's going to attempt to ask the user for an integer.
So I'll say the following.
Try to result is equal to integer of the input please.
Provide number and then if we get an error here and going to say except and I'm going to print out whoops
that is not a number.
And then I'll say finally print and of tri except finally.
So I run this and we're going to iterate on this function just a little bit but I'm going to call this
function right now.
Do shift enter.
It asks me to please provide a number.
Let's give it one 20 and I just get and the try except finally it makes sense that whoops that is not
a number that imprint because we were successfully able to cast or transform 20 into an integer.
But what happens if I run the cell again.
And now I provide a word.
Now I know I definitely can't transform the strings word into an integer.
So when I run this it says whoops that is not a number.
Now we get an end of try.
Except finally.
So let's see how we can now use a while loop to continually be doing this try block of code.
And then except if there's an error there.
OK so let's see if we can adjust this function so that instead of just ending after you provide the
wrong word we're going to put it inside of a while loop that keeps going over and over again until you
actually have a block of code with no exception.
So I'm going to do it while true and you should always be careful in doing while.
True because it basically means you need to use a break statement somewhere to break out of this while
loop.
I'm going to indent all of this highlighted all hit tab here and I'll say wild true.
Get this result as the input then I say except whoops that is not a number.
And then I'm going to say continue to make this a little more readable.
As far as the logic what's happening now let's say there is not a error then I'm going to say.
Else you can actually combine Elswyth finally here.
So we're going to try this block of code.
If there's an error I'm going to say whoops that's not a number.
Continue on else meaning there is no exception.
That means the user did provide an integer.
So I will say yes.
Thank you.
Now I need to break out of this while loop.
So I will say here break which remember the break statement goes up to it's inclosing loop either while
loop for loop and then ends it.
So end this while loop.
Then finally we'll say and a try except finally.
OK so we just put these four blocks of code inside our while true and we're going to break once we say
yes thank you and we'll have a Finally statement that says print and the try except finally and we can
just make it very clear that Prince I will always run at the end.
So regardless there's an error or not this finally block of code is always going to run.
So we're going to run this ask for an integer.
Let's provide a correct integer we'll say 20 hit enter.
It says yes.
Thank you.
And the try except finally I will always run at the end.
Let's provide it.
A string will say q hit enter and it says whoops.
That is not a number and the try.
Finally I will always run at the end.
Except we have while true here and this break hasn't actually executed yet.
So we have another chance to provide a number will say.
Q It says Whoops again.
And you can keep providing it over and over the wrong things and it's going to keep asking you for input
until you finally provide something that being kept that can actually be cast as an integer.
You say 90 and it says yes.
Thank you.
And the try except Finally I will always right at the end.
So what's happening here we're saying while true try this block of code if there is an exception do
something and then continue else meaning there is no exception.
You can print some sort of statement and then break.
And typically you're only going to want to break out of this while true when there is no exception.
Then finally if for some reason you wanted something to continue.
So a good thing here could be like I'm going to ask you again.
New line.
So that's more realistic example of why you would say finally.
So if I run this again they'll say please provide the number.
We give it the wrong answer.
It says that is our number.
I'm going to ask you again please provide a number and then you can provide it the correct number.
And it says yes.
Thank you.
Unfortunately it's going to say I'm going to ask you again.
So that's why a lot of times people don't like to mix up else.
And finally statements but there's the option for you in case you want it where you could do instead
of just get rid of that finally statement and when you're code like this and you'll say please provide
my number.
Q Well that is not a number.
You can keep doing this over and over again until you actually provide the number and says Yes I think
you it's up to you whether you want to mix Elsin finally blocks but you can if you want to.
All right.
That's the basics of try.
Except.
Else.
Coming up next we're going to give you an assignment to test your new skills on this.
We'll see if they're.