Hello guys.
So we are going to continue our discussion with respect to Python.
And in this video we are going to discuss about exception handling.
Right.
And I hope you have heard about this particular term.
And uh, what exactly is an exception handling?
Exception handling in Python allows you to handle errors gracefully and take corrective actions without
stopping the execution of the program.
Right.
And uh, here in this specific lesson, we are going to cover, uh, and we'll be seeing some of the
keywords like try except else and finally blocks.
And we will be seeing how we can handle the exception that arise in our program.
And we have to handle in such a way that we do not break the entire execution of the program.
Right now.
There may be scenarios.
Let's say that I have I'm using an application over there.
A user is filling some kind of form and it does not give the right kind of input.
So in that particular case, I will not just stop the execution of the program.
Instead I will say, hey, I will raise an exception.
I'll tell the user, hey, try to put some proper information over there, right?
And make sure that you always try to put this kind of information to make that entire form get submitted.
Okay.
So we are handling it.
We are handling it gracefully.
Right?
We are not stopping the execution of the program.
Now, let us go ahead and understand what are the exceptions.
And you really need to understand the differences between errors and exception.
Exceptions are events that disrupt the normal flow of a program.
They occur where an error is encountered during program execution.
Common exceptions include zero division error.
So here we have something like divide by zero file not found or value error type error.
These are some of the examples.
Okay.
Now let me just show you uh like uh uh I'll just show you some amazing examples.
And how do I handle it?
With the help of exception handling, I will try to talk about it.
Okay, now let me do one thing.
Okay.
Let me just go ahead and write a is equal to ten okay.
So this code I think it will work perfectly because we have assigned the variable.
But if I go ahead and write e is equal to b and if I just execute it.
So what is the error that I'm getting over here.
Write error is very simple.
It is saying hey name B is not defined.
So what has happened over here?
This is a kind of error that has actually happened.
And as I have already told, they occur when an error is encountered during a program execution here.
Obviously I know the error is like name B is not defined.
So an exception is basically getting raised over here.
And this name error is one kind of exception, right?
It is one kind of exception like how I have written over here.
Zero division error file not found error value error type error.
So name error is also one kind of exception that is probably getting raised over here because we have
not defined B okay.
So how do I handle this.
Let's say that I'm not defined B over here.
And obviously this kind of error that I probably see it is suitable for a coder.
Right?
A coder will be able to understand.
Okay, fine.
In this particular line of code I'm getting this kind of exception.
Right.
But if a user if a user is using some kind of application, if he or she sees this right, they will
not be able to understand.
So it is always better that we handle it gracefully by providing a good message.
Right?
So how do I handle this?
So what I will do in order to handle exception handling right.
Let me just go ahead and write.
I'll be using over here try accept except.
Try except blocks.
Okay.
It is just like try catch block in other programming language.
So first of all, I will go ahead and write.
Try.
Okay.
Now what does try mean?
Try basically means inside this block I will go ahead and write my block of code where exception may
arise.
Okay.
So let's say right now the exception was rising.
In this particular code write a is equal to B.
Right now What I will do is that when I write a is equal to B, right?
Obviously we know that an error will come over here.
Now in order to handle this error, what I will do, I will just go ahead and write accept.
Okay.
And I will just go ahead and write my print statement.
I'll say, hey, the variable has not been assigned right.
The variable has not been assigned.
Okay.
So this is my custom message that I'm saying hey if this particular error see when this code gets run
here exception got raised right.
And this exception will be caught by this accept block.
And when it will get caught here is the print statement.
This is the print statement that will get displayed right.
So if I go ahead and execute it once again.
So here you'll be able to see hey see the variable has not been assigned.
Now this is a very good message when compared to the previous message where we had read statements.
And all for the end user.
Right.
And obviously we know that what is the kind of error that we have got over here.
A is equal to b, c this is the error that we are getting.
Name B is not defined.
It is not like that.
I cannot display this.
I can also display this.
Let me show you how we can display this.
So what I will do I will just copy the same thing.
I will paste it over here.
I know what kind of error or what kind of exception it is throwing.
It is a name error exception.
So I will go ahead and call this same exception.
Okay.
This is one type of exception over here.
Right.
And let me just go ahead and make my alias right.
And I will just go ahead and say hey print X okay.
So what I'm actually doing when this kind of error is falling, right.
When this kind of error is getting raised, a name error is getting raised.
I am handling those kind of exception with the same exception handler over here.
Right.
And this name error is a class which handles this kind of exception.
Right.
I hope you are able to understand it.
So now if I go ahead and execute it see the same message is getting displayed.
Name B is not defined.
And here also we are getting name B is not defined.
Why?
Because I have used the same class right.
That is name error which is responsible in catching this specific exception.
Right.
So this is one amazing example which you can easily understand right now.
Similarly I may have other kind of exception right.
So let's go ahead and do this.
I will go ahead and write hey let's do this okay I will go ahead and write.
Result is equal to one divided by zero.
If I execute this you see what kind of error is coming right.
This is called as zero division error right.
Zero division error.
Now zero division error is again a kind of error that we are getting at.
And this is also kind of an exception.
Right.
And we can also handle this exception.
How do we handle it.
I will go ahead and write my try block okay.
And here my code will be there.
Now I know if this exception gets raised I'm going to basically called except uh, except and my exception
handler will be or my class name that I have to use which will cast this kind of exception is zero division
error as x.
Okay, X is just an alias.
And we will go ahead and print x.
Okay.
Now you can see division by zero right.
And apart from this I can also go ahead and write another message saying that hey please enter a number.
Please enter the denominator.
Denominator greater than zero.
Right.
So I can go ahead and write this.
So this way I am able to get more beautiful messages.
And I'm handing handling it gracefully.
You know what I say.
What do I mean by gracefully?
I'm not stopping the execution of the program, not giving that entire read section that you always
be, always are having a fear right?
Of that.
I'm giving a proper message over here saying that, hey, this is the exception that got raised.
You need to fix this.
Please enter the denominator greater than zero.
So again, the person will go ahead and make sure that he'll understand.
And then they'll go and write this particular denominator greater than zero.
Right now all this exception one more things right.
So here you can see that all these exceptions that are there like zero division error file not found
error value error type error.
These are derived from a main exception class Okay, so let's say that I don't know, there may be some
kind of exception that may be happening.
Okay.
Um, let's say that over here again, if I go ahead and write a is equal to B, right.
And let's say I am going to just write one by two.
Now here you will be seeing that.
Okay.
Or let me just go ahead and write like this.
I will copy this.
Let's say I go ahead and copy this and I go ahead and write one by two.
So obviously zero error is not going to get caught.
Right.
So now if I go ahead and write a is equal to B now I know A is equal to b is going to give me a name
error.
But will this exception get caught?
Right now the answer is no because I have just written zero division error over here and I'm catching
it in the accept block.
Other than that, I have not used name error, right?
This exception handle this exception is never handled it with the help of Nameerror.
Right.
So I have not written it in the except block, right?
So that is the reason I'm getting this particular error.
Saying that the name be is not defined.
Right.
So how do I handle it.
So see one more thing is that and sometime it happens in such a way that they may be different different
exception that may be occurring.
Right.
And all these exceptions that we learn like zero division error or name error value error.
This is derived from one single parent class.
Right.
We'll talk about more classes and all that is nothing but exception.
This is the base class of exception.
It can handle any kind of exception.
Right.
So if I go ahead and write as x one okay.
And now let me just go ahead and print x one.
Now we'll see.
Since I'm getting an error over here obviously you will be seeing that this exception is not getting
handled over here because this is a zero division error.
But this exception will probably handle because this is the main parent class of exception.
And every exception will be derived from this particular exception class.
Right.
Like zero division error will also be derived from this name.
Value error will also be derived from this.
So at the end of the day you'll be seeing that this exception will be handled over here.
Right.
So if I go ahead and execute it once again.
So here you can see name B is not defined from where it is basically coming.
Right.
So here it is basically coming from right.
Main exception class main exception got caught here.
Right.
If I execute it see it is getting caught over here.
So you can write any number of exceptions, but always make sure that the last exception class that
we are using should be in the last section, right?
We should not write this before zero division error or any other kind of exception, because this is
the parent class from which all the exceptions are basically getting derived.
Okay.
And this is how we handle different kind of exceptions.
Right.
So I hope you have got this particular idea.
But here what we have actually done is that we have handled multiple exceptions.
Right.
So let me just go ahead and show you one more example.
Let's say I go ahead and try over here.
I uh I'll create a number okay.
And this will be my int.
Okay.
Let me just go ahead and write input.
And here I will go ahead and write enter a number okay.
Okay.
Now once I enter a number over here, let's say that I go ahead and divide.
This result is equal to ten divided by num okay ten divided by num.
Now I will go ahead and catch or write this except block.
Remember.
Except block is basically to handle all the exception that arises inside the strike try block right.
So I will just go ahead and write accept.
Okay.
Now inside this accept, uh, let's say that I will go ahead and use one value error.
Okay.
Uh, value error.
And uh, here I will just go ahead and print.
Let's say value error is specifically if I do not write a proper number over here.
Right.
So this is not a valid number.
Not a valid number okay.
Similarly I will go ahead and write accept another except block.
And let's say here I'm just going to use zero division error okay.
And this will basically say hey print print.
You can't enter number enter denominator greater than zero okay.
Now see how I'm handling right.
This is the block of code where the exception will be handled.
Uh exception will be raised.
Right.
Um, and this exception, like error will be coming over here and we will catch this particular exception
either over here or either over here.
Right.
So let's see this.
So if I go ahead and execute it, let's go ahead and say that I've given the number as zero.
Now what will happen if I press enter.
Just imagine obviously zero division error will get caught.
So here you can see enter denominator greater than zero right.
Let's say that if I go ahead and execute it and this time I give a string I give a value called a string
crush.
So now what will happen.
The value error will get caught.
And this is not a valid number right.
So here clearly you can see that I'm able to handle it.
Now there may be different different kind of errors.
So what I will do finally I will just go ahead and write accept with exception as x.
And I'll just go ahead and print X okay.
Over here just to manage all the other errors.
So let's say if I'm giving 34 this will get executed perfectly because I'm dividing the result by ten
by that specific number.
Okay.
So here was an example to make you understand about try catch block.
Or we also say try except block okay.
Now I just want to also show you one more example.
And here along with try except I'm also going to show you else block.
Okay.
Now let's go ahead and see this.
So here is my try.
Okay I will go ahead and create my number.
And this will be input.
And here let me just go ahead and write.
Enter a number.
Okay.
Enter a number.
Then I will just go ahead and say result is equal to ten divided by number.
Okay.
Let's say this is there okay.
Now one exception that I'll try to handle is is value error and zero division error.
Okay.
So I've written it over here.
Let's say if both this error are not coming that basically means this has got executed successfully,
right?
Uh, in the worst case, I will also go ahead and write my exception.
Okay.
So that basically means if this is also not getting caught over here, then that basically means the
code has got successfully executed.
Okay.
Without any exception.
Okay.
But finally, let's say that I want to display this particular result value okay.
Now the result value can be displayed uh, over here.
And I will try to use one.
amazing, uh, try catch functionality right over here, which is called as else.
Now, let's say after this particular code has got executed, if exception is not happening, I want
to close this entire try try except with with making sure that some lines of code needs to be executed
anyhow.
Okay, so there will be an option of writing else block.
And this try is, uh.
This else block is nothing.
But it is.
Uh, you can just say that, okay, at the end of the day, after this gets executed successfully,
it has to execute this.
Okay.
So if I go ahead and write in the else so let's say in the else, I go ahead and say, hey the result
is this specific result okay.
So I'm just displaying the result over here.
Right.
So if I go ahead and execute it now see what will happen okay.
Now what will happen?
I will give the input let's say 23.
If I press enter okay.
You'll be able to see 23.
No exception will be raised.
Now when no exception is getting raised.
So obviously this will get skipped.
But at the end of the day this has to get executed right?
So else is something like it has to probably get executed.
Let's say if there is an exception over here, then one of the exception block will get executed and
it will come outside of this try except block.
But if this gets successfully executed anyhow, it is going to execute this else block.
So if I go ahead and press enter.
So here you can see the result is 0.434.
Okay let's go ahead and execute this once again.
And let me just go ahead and write zero.
Now in case of zero what will happen.
Zero division error is there.
So you can't divide the number by zero.
So I'll press enter.
That is the error.
But here you can see else block is not getting executed.
So that basically means whenever an error happens or exception happens it will get handled in the except
block at that point of time, else block will not get executed.
But if none of the exception gets raised over here, then what will happen?
Else has to execute anyhow.
Okay, so this was an amazing example with respect to else, right?
So where uh, you'll be able to see that Okay.
If I am not getting an exception, then it will get it will execute.
Okay.
Otherwise, uh, if I'm getting an error, this is just going to stop.
And it is not going to execute this else block.
Okay?
This is how you will be able to understand.
Now this is fine.
Let's try something more.
Okay.
Now this time what I'm actually going to do I'm going to use try except else I'm also going to use one
more, one more amazing functionality or one more block, which is basically called as finally.
Okay, now let's see one example with respect to this.
Now what exactly is finally we'll discuss about this.
Okay, I will just write a code and the same kind of code.
I will just copy and paste it over here.
So see this okay.
So here is my number.
Right.
And here I'm probably dividing it.
If exception does not happen over here, then directly else has to get executed.
Okay?
Else will get executed only when error is not there.
Okay.
Again I'm.
Let me repeat it.
This is going to execute.
Get executed only when there is no error over here, right?
If there is an error, it will be handled by this kind of exception.
I'll also go ahead and write this exception over here, so that I will bring up multiple condition of
exceptions.
Okay, now finally, something finally is a kind of block weather error may be coming or not in this
particular block of code.
It has to execute anyhow.
Okay, so let me repeat it again.
We have already discussed just a while back about else.
Right else is something like this.
If no error is probably coming over here, then we will go ahead and execute this.
If error is probably coming up, then else part will not get executed since it is getting handled in
the exception block.
In case of finally block whether the error may be coming here or not, it has to execute.
Okay, now let me just show you.
So let's say if I go ahead and write 12 in case of 12, obviously it is a good number.
It is a proper input.
So no error is getting handled.
So else block will get executed.
The print result something will come and after this finally block will also get executed.
So if I go ahead and press enter.
So here you can see the result is 0.833 and execution complete.
Okay.
Finally is also getting executed.
Now in case of, in case of uh, let's say in the case of error, let's say if I go ahead and write
over here zero.
Now in case of zero, what will happen?
Zero division error is coming up.
Right.
So x else block will not get executed.
But finally block will definitely get executed.
So if I go ahead and press enter you can see you can't divide by zero.
That is zero division error.
And finally basically gets executed which is called as execution complete.
Now you may be thinking, Chris, where in the real world scenario this will come, right?
Where do we specifically use final find block at all?
Let me just give you one example guys.
Let's say that in try block I am connecting to a database okay.
Now in this particular database let's say I got some connection issues, user password issues and.
All right um, or let's say that I have connected to the database.
Then I did some kind of operation there.
I got an exception.
I handled that particular exception right.
But at the end of the day, I need to close the connection of that particular database so I can write
the code over here in the finally block.
Right.
Because at the end of the day, if I'm getting an exception.
So that exception is basically getting handled.
But if I come out of that particular block, I need to make sure that that entire connection gets closed.
And that is how we can specifically use this try except else and finally block.
Okay.
Now this was some of the ways of handling exceptions.
And again any kind of errors that you'll be seeing there will be different different exceptions which
you can probably check it out.
Okay.
Uh, again uh, guys, uh, the thing is that unless and until you don't practice, you won't be able
to become perfect.
Please make sure that you have this all indentation techniques that is basically getting aligned.
You can have multiple exception blocks.
You can have multiple, uh, but you can also use this else and uh, finally block also to handle more
diversified code errors.
Okay.
Now let's see some kind of practical examples.
So first of all I will just go ahead and show you file handling and exception handling okay.
This is most commonly used okay.
So let me just go ahead and use a try block I will go ahead and open my file.
So let's say file is equal to open open.
Now you remember right in in previous videos when we were discussing about file operation, we tried
to open a file which is called as example dot txt, but that was not present over there and it gave
us an error.
It gave us an exception saying that hey, this file does not exist.
Okay.
So we'll try to, uh, you know, do the same thing over here.
So here I will just go ahead and open my example one dot txt file okay.
And here I will open this in read mode.
Let's see.
And right now if I go ahead and show you in exception handling there is no file called as example one
dot txt okay then uh I will just go ahead and write.
Content is equal to file dot read will read the file inside that and we'll go ahead and print the content
okay.
So this is my try block where the exception may rise.
Right now I will go ahead and uh handle this exception, so I will I know that this is a kind of file
not found error okay.
And because if the file does not exist.
So this kind of exception I will be able to see.
So here you can see the file does not exist okay.
Okay.
Now, anyhow, uh, in the finally block, let's go ahead and write this in the finally block.
In the finally block, I'm going to make sure that at the end of the day, I need to close this particular
file.
Right.
So I'll write if file if file in locals.
Okay, now what exactly is locals?
Returns a dictionary containing the current scope.
Local variables.
Okay.
If it is not present in the local okay.
And the file is not closed.
Okay, I may get an exception over here also in the read also.
Right.
So that is the reason I'm writing this particular condition.
If this two are if if that file is present in the local variable and it is not closed, I will go ahead
and close this file because I cannot make this file open.
You know, there may be some kind of issue similarly with respect to database connection.
So I will go ahead and print file closed okay.
At the end of the day, whatever may happen in the exception right in the finally block I am going to
close this particular file.
Now, if I go ahead and execute this, you'll be able to see the file does not exist is coming right.
So here automatically the exception is basically getting handled.
Right.
Um, and let me do one thing.
Let me just write a is equal to B over here.
And let's say that okay fine I have some kind of file okay.
I have some kind of file.
Uh let's say here example one dot txt.
Okay I have this file.
It is an empty file.
Now what will happen when I write e is equal to b?
I know it is not a file found error, so I will just go ahead and handle it with the parent class exception
as x.
Okay.
And I will just go ahead and print x okay.
But at the end of the day I know the code will be giving me an error over here, but what I am doing
because here I will be reading the file, right?
So the file is in the open state yet.
So when this error comes, I know this will not get handled by this, but instead it will get handled
by this.
In the finally block, I am making sure that I'll close the file.
Okay, so that is how we will specifically do so.
Here you can see that I am actually getting an error over here now instead of this.
And I will just go ahead and use.
Ah.
So once I have used ah here you can see name B is not defined and file closed okay.
So so we are at the end of the day we are just closing all the specific files okay.
Uh some kind of errors will definitely be coming up.
So we should always make sure that whenever we are opening the file, at the end of the day, we need
to close this file.
So I hope, uh, you are able to understand this, uh, entire thing and you are able to understand
with multiple examples.
I have shown you each and every thing step by step.
We have executed each and every thing.
So yes, this was it for my side.
I will see you all in the next video.
Thank you.