Welcome, everybody, very happy that you're here with me and we're going to talk about the second part
of exceptions and error handling so and the previous video, we have seen several examples of popular
errors that can be raised in Python language.
And we have seen like what the code that causes these errors to be raised.
And also we have seen this sibling error which was raised in the terminal below in charm.
We have seen the name of the error.
And we also see that it points out to exactly where the error happened.
So that was kind of the negative part that our code basically can fail and it can stop being executed
because we also mentioned once by chance encounters an error.
It basically stops from running.
So every code that you are going to have below the line that caused the error would not be executed.
So this is kind of like the pessimist side of the topic of errors.
But today we're going to talk about the positive side of exceptions because exceptions can be managed
and basically we can have full control overall code and prevent it from failing.
So in Python, we have this tri except statement, which will give us the full control once we have
an error in execution.
And this part basically tells us that, as we just mentioned, that the immediately the execution stops,
once it encounters an error, but we can't have a full control of our code and stop it from failing.
All right.
And we're going to use the tricep statement to do it.
So we're going to discuss about the code flow.
Basically, what are the things that are happening behind the scenes once we use this dry except statement?
So we're going to see what happens with the code flow and also going to have a quick implementation
example where we see exactly what happens if an error is encountered in the execution.
All right.
So you can see here how dry except looks like you basically going to have a try keyword in some code
inside it, same as we had in four loops and if statements.
OK, so here we have dry statement and we have certain code inside it.
And right after you have a sibling, except and you also going to have some code inside this one as
well.
It's important to mention that you have to use these both together.
There are siblings and if you only write dry and not mentioning except in your code, you are going
to have an error.
So always use both of them together.
Now, let's talk about the dynamics between these two.
So let's start from here once we run a file that has dry except statement, quote, execution will first
run what's inside dry.
So first of all, the code will go inside the dry.
And if you have any variables or print print statements in here, it will run it.
Whatever any code you right here, it will run it.
And then you have two options.
If no errors would be raised in my statement, the code will completely one, sorry, the call will
complete the execution within try and we will ignore the part completely.
OK.
So if this part in here and so dry, you do not have any errors.
So it will complete the execution inside dry and will ignore the accept part completely.
And then whatever is after whatever appears, whatever code appears after dry would also run.
OK, this is like the usual stuff, you know, like once the cold inside dry will be executed, if you
have any code here below, it will also be executed.
OK, so not just going to stop and dry, except like the normal behavior of code.
If you don't have any errors, it will just simply run.
All right.
But if you have an air or reste inside this dry statement right here, the cold will not fail or stop.
And this is the important point we need to stop and discuss about, because in the previous video we
have seen, we have created some code which cost us an exception.
And then the execution basically stopped immediately and it did not continue to run.
Just wrote to us in the terminal below.
We have an exception and the code stopped from running.
But in this case right here, the code that inside Trie had an exception and it did not stop the execution
basically jumped immediately.
And it's starting to execute whatever is inside the except.
OK, so the execution will instantly move to executing of what's inside.
Except, OK, so this is the whole deal.
This is how we get full control when having an error during execution, because if having some problematic
code, we should, you should think about putting the problem code or dangerous code inside dry and
then if it gets an error it will immediately go to accept and it will continue to execute our program.
So that part was regardless, regardless of implementation, it basically was a simple code flow explanation.
What happens inside here and now we're going to see an implementation error of what really happens if
we're encountering an error in live code.
All right.
So this example right here, you can see declaration of try and accept using these two keywords and
then you have some indention inside attrite, same as you have inside.
If statements are for loops and just need to have some indention.
And right here we having this if age is smaller than twenty.
Yeah.
The if statement while we see that we did not declare the edge of a variable before, OK, we're just
using age smaller than twenty, but we did not put any value to age.
So basically this causes a syntax error.
OK, this execution of this card will cause a syntax error.
And this is the first example that we have seen and the list of errors in the previous video.
But in the previous video, we did not have dry, except we just had this if some number smaller than
some value.
We had an exception because we did not declare the variable and then the code stopped from running.
But this time we're having this variable inside.
Try and let's try to apply the rules that we have went over here to this example.
So one basically will happen when the code reaches this if here this print would not be executed.
As we mentioned that when having this exception in this line right here, it immediately jumps to the
accept part and the code will execute whatever inside the except ignoring the code that comes after
the exception right here.
And try.
And this exception perhaps here, age variable was not assigned with any value setting value.
Three now.
OK, I'm doing whatever I want here.
I have full control over the code.
Basically, I recognized that I'm going to have a syntax error here.
So I immediately know the reasons I wrote here to the end user.
Whoever runs the program, each variable was not assigned with any value setting value three now.
And once I encountered this error, basically here I'm giving a value off three to the age variable.
And then I'm asking again, if age is smaller than 20 and if it's true, then this person is very young.
So we basically redirect the code.
Once an error is raised by China will continue the run from what's inside.
Except I think this covers the dry accept and error handling pretty good.
The topics we have covered.
We have seen lots of examples of exceptions and also now we are familiar of how controlling the code
and stopping from the exceptions of failing our execution.
All right.
Let's see the following, the actual regarding our an assignment, the homework assignment regarding
off tricep.
And there I will give you a preprepared bulk of code that would raise a certain exception.
And I would like you to use tricep to overcome from that cold from failing.
I will see you in the next lecture.
Thank you.