Hi, everybody, welcome to the lecture.
We're going to start off with a new topic and we're going to discuss exceptions and errors in the code
once we have completed the challenging part of OLP.
Now it's time to know how to handle exceptions and errors in our code.
In a perfect world, a programmer would create a piece of code that will run perfectly and would not
have any fair points or errors.
But in the real world, in real programming projects, as they grow, they have more and more potential
to have exceptions and errors in them.
And we better know we better be prepared to handle them and not leave it to luck and hope that we would
not have these errors.
We would need to have a sort of prediction ability to know how to catch these errors or even fix them
in the code once we encounter them.
In computer science are once the program encounters an error, it is called an exception.
All right.
So let's go with the slides here and see the following examples.
An exception would happen once a program is executed and it would encounter a wrong piece of code.
By the way, it can come in many ways and shapes and we have many types of errors and we will go over
the common ones in a few minutes.
So when the code execution encounters the wrong piece of code, it will cause the code to fail at that
point.
Python does not know what to do with the defective code, and it basically terminates the execution
on the spot.
Biden also knows exactly what type of exception is thrown once it happens, and then we can apply certain
actions to either get a flow of code, to do something, to do some commands or either to fix the code.
So how do we know that our code is failing and that we had an exception, so once we run a certain Python
file and the code will encounter an exception, the run will stop.
So by the look of our picture, if we're looking at our code stopped in the middle, it's probably because
we had an exception during the run.
If the code flow will encounter a badly written code, it will cause the run to fail and the error would
be displayed at the terminal log off by chance in red color.
So once we are executing a code and we will encounter a certain exception, it will be printed for us
so we could read and debug it.
And to understand what that means, the terminal would also give us indication and what class method
or module the exception happened and also would point us to the exact line in the code so we could look
at that problematic line and fix it.
So once we are familiar with, like, what are the signs that we had an exception and where can we find
and read that exception, let's see a list of exceptions.
And I picked up the popular ones because there are many of them.
And covering all the exceptions is irrelevant, because once you get the idea of what is an exception,
even if you will encounter is some exception that you're not familiar with, you could go to Google,
read about it and learn more.
But we're going to talk about the common ones.
I picked that up for you and you will see some examples as well.
So let's begin first.
One is syntax error or parsing error.
This error will because once the code will encounter a programmatic logic of failure or once we will
not fall to Python syntax rules for the example of that syntax error or parsing like the come as a power
you can see here.
And if statement if A is below, then three.
But it was caused because we did not put any value in a OK.
We did not declare the variable before using it.
So the terminal would give us this exception right here.
And we could see here that it happened in this file, Eurhythmics exception UI.
And you can see here that if you would click on it.
So first of all, we know the name of the file and we have also indication what line happened.
Line one.
And also, we have a small error pointing up here, pointing us to where exactly the exception happened.
So it's basically here in the statement of values and also we have the exception itself, syntax error.
Right here, and we need to fix it, so this is for syntax error.
Let's see the second example of zero division error.
OK, the name speaks for itself.
Basically, we are getting this exception once we're doing an illegal mathematical action, which is
dividing a certain number by zero.
So in the real world, regardless of programming, you cannot divide anything by zero.
It's an illegal math action.
So Python will tell us that zero division error division by zero.
OK, so this is also an exception.
This will cause our code stop running.
So if we have these two lines and then we have a hundred more lines, the code will stop here.
Third one in line is tie power, and it will occur once using a wrong type of object and it can appear
in many scenarios, I just gave you a one here.
We already encountered this scenario many times.
In the course we have here, X equals five, and then we're trying to print a string together with an
integer.
And this would cause us the type error you can see here can only concatenate string nuttiness to string.
And also pointing out here the line and also pointing out the name of the file.
So how can we fix it?
We already did it many times.
We should fix it by using casting in that order.
All right, fourth in line, name error name is caused when a variable cannot be found in local or global
scope.
So here we have a math accident, X plus five, but we never created X, so Python does not know what
to do with the statement.
He's stuck, he's confused, and he's going to fail the execution once we do that.
So we, first of all, need to create a variable and then to put value to it.
And only then we can use in the math section.
So we never created the X here and we have here name error, name X is not defined also line number
and filename as well.
Index error is caused when reaching out for a sell off collection by index and that index does not exist.
OK, so we have a list here that at least have three.
Index three is the last one.
And here we try out to pull index six, which is.
Does not exist, basically, so we have index our list, index out of range.
All right, so we have indention error, as you already know, Python has indention rules and these
inventions, they basically give us the hierarchy of the code and the flow of code.
And we need to keep you keep implementing according to indention rules.
So in this case, you can see here that equals force equals four does not have an intention, but the
print afterwards has indention, and that's wrong.
This will cause our code to fail and raise the exception in the error least index out of range.
Kierra wants to try to call out a dictionary cell with a wrong or nonexistent key, so we have the key
here, A, B, C, the value one, and then we try to reach out by using the key Z, and that key does
not exist.
So we're going to get an indention error.
Unexpecting indent eight one in line value error will be caused by using a wrong value, a value that
cannot be used in a certain command.
So we're trying to cast the string dog.
Dog is obviously letters and we're trying to to make it to convert it to Cassity to an end.
This cannot be possible.
And we're going to have this exception value for dog.
OK, so these items were the common exceptions that you're going to encounter in your code, basically
you're going to have many more exceptions in general in Python.
And once you encountered the you just Google it and you find the exact explanation why the exception
happened.
And once you know why, you can either fix it or to overcome it in some different techniques.
Thank you.
And I will see you in the next lecture.