Hello guys. Welcome to Learnvern. In the last topic we saw File Handling.
We saw how to handle the files in Python & how to store data in files.
Our today’s topic is Exception handling.
What is Exception Handling?
How to use Exception Handling? In what way can we handle multiple exceptions?
We will see all about it in today’s video including both, the Theory & the Practical part.
I’ll explain Practically & make you understand the theoretical part easily as well.
First of all, we will see what Exception Handling is.
Exception Handling is an abnormal situation which occurs during runtime which causes the code to stop there & then.
But what is an Abnormal situation?
Suppose you’re driving for 1 or 2 hours continuously & your car breaks down unexpectedly or a tyre is punctured.
It means that your car will come to a halt, you’ll have to stop the car.
This works in the same way...you’ve written your code perfectly or run it perfectly and it even is working properly but all of a sudden there is some wrong input, it will terminate your code, stop the code.
That stop card is called an Exception.
There are 2 types of error in Python - 1 is Syntax error and another is Exception itself.
Let us understand what is Syntax error & what is Exception.
Syntax error means if you’ve written a code but you forgot to close a bracket somewhere or forgot to apply colons, or wrote the Syntax is a wrong way, it is one type of Syntax error which can cause your code to terminate.
Meaning if you’ve written a 3-4 lines code perfectly, opened a bracket before but forgot to close it.
If you didn’t close it, it is a syntax error.
I’ll explain all of this practically to you.
Now what is Exception?
Suppose you have written the whole code properly, even the syntax properly but as soon as you run it & you had asked for integer input but your client entered a string, the wrong value was entered which made this an Exception.
This exception can terminate your code as well.
Next is User Defined Exception.
‘There is one exception which is generated by the system & the other is the one generated by the user itself.
User Defined Exception is the one created by the user to handle any particular error.
You have to inherit the Exception Class whenever you generate a custom exception in Python.
Let’s go to practicals to see how it is used & how it works.
Jupyter is opened.
We will see all about the Exceptions in this. (
I’ll write Exception Create.
I’ll write a Code where I took the variable A.
And input Int. and write Enter the value of A.
Asked a value from the user and printed the value of A.
Now what kind of value have we asked? We have asked an integer.
I entered 10, and it is working perfectly.
Suppose I’m entering a string here named Python.
I entered a string in integer value input.
Did you see we got an error?
The message is Value Error.
Value Error is caused because we entered a string in an integer input value, it stopped the code & directly gave an error.
Now how to check if our code actually stopped or not?
I’ll simply write Bye here to know it stopped.
I wrote 10 here. It showed Bye which means our code is working perfectly.
I wrote Hello here. It gave an error & our Bye didn’t print either.
We got a wrong input by the user.
So in place of Integer, when we placed a string, it gave us a value error.
We will take 1 more example.
I’ll take another input here.
A = int input. Enter the value of A.
I’ll take another input in the same way with B.
Change the variable name too.
C = A divided by B.
I’ll print the answer here.
The answer will be stored in C.
Lastly, I’ll print Bye.
I took 2 inputs here, divided them & printed the answer.
Now I’ll run it.
10 divided by 2, the answer came 5.0 and then Bye.
Which means this code is running perfectly.
I’ll run it again. 10 divided by 0.
But no value will be divided by 0.
It gave us an error, here.
An Exception occurred here.
Exception occurred on runtime because division by 0.
As it is divided by 0, it gave us an exception here and said your value cannot be divided by 0.
The code should not stop due to such errors even if the user has given wrong input.
I’ll copy this code to bring out a solution.
Here, we have to use Try & Exception.
Let’s see how to use it.
We have to use Try Block.
The code which is written in Try block can get errors at runtime.
Meaning whichever code I’m going to write, might get errors.
I have taken this code in Try.
Now when you’re going to handle it, you have to write except.
You have to call the Exception class.
The main Exception class as E.
And now we will print Exception Caught. And E.
This block will run when Exception occurs.
And wrote Bye at the end.
Now we will run it. I’ll write 10 here & 5 here.
I got the answer 2.0 and then bye.
Now we will run it again...with 10 & 0.
Did you see that the division by 0 is caught here as an Exception.
Meaning this block did run, then only it printed the Exception caught and E.
And most of all, our application did not stop.
It did Run till Bye.
We handled the exception with its help on runtime neither did the application stop.
Moving ahead to see more ways to work around it.
Now suppose you have to handle multiple exceptions.
Not multiple, many exceptions.
It is very simple.
Suppose I took a try block.
And I directly printed X here.
I took an exception block here.
I simply wrote here the NameError class.
I printed it. Print variable is not defined.
And 2nd exception, except.
Print Exception Caught.
When I run it, Exception Caught found this block.
Why? Because…
I’ll write a comment here “We have not defined variable X”.
We have not yet defined variable X & are printing it directly.
It caught the Exception & stated variable not defined.
This is how you can use Many Exceptions.
Suppose you have written some code & the exception got caught, it will be under this part.
And if you have not defined any variable & printed it directly, it will go under NameError & “Variable is not defined” will be printed.
So this is how you can use Many Exceptions.
Moving ahead…
We will see how to use the Else part with Exception.
How to use Else with Except.
I took a Try block & printed Hello.
Here I took an exception & printed “Something went wrong”.
Python gives one such benefit that you can use Else with anything.
I wrote Print Nothing went wrong.
Suppose an Exception is found, then it will go in Except & the result will be Something went wrong.
If not, it will print Nothing went wrong.
It printed hello & nothing went wrong. Meaning there was no error found.
Now suppose instead of Hello, I directly printed X, the result is Something went wrong.
This is how you can use Else and the Except part during your exception handling time.
One more thing-
Finally Blocked.
First of all, we will see what is Finally Blocked?
The block which runs compulsory if an error occurs or not.
This block runs compulsory even though there is an error.
It will surely run as it doesn’t matter to the block.
I took a Try block here. & printed Hello.
Except. And I wrote Print “Something went wrong”
You just have to directly write Finally in the Finally block.
Print Finally Block.
What does Finally Block do? See, when I run it, it runs smoothly without any error.
It got printed as well.
I’ll make a comment here & generate an exception.
“Something went wrong” got printed but still our Finally got printed.
What is it used for? Suppose you want to close a particular thing after opening it & you have to close it every time ofcourse.
So if you write it under Finally block, it will automatically get closed.
Hope you understood how to use the Finally block.
Now how to make user defined exceptions?
User Defined Exception, is used to make customised exceptions for users.
First of all, you have to take class. Class is one type of keyword in Python which comes under object orientation.
I’ll show you in future videos how to use Class.
I took Class & named it MyException.
Class is the keyword & MyException is its name.
And I inherited it in this class.
I’ll show what inheritance is in the videos ahead.
I inherited the Exception class which is predefined in Python.
You don’t have to write anything, you only have to pass it.
We have already used Pass in the previous videos.
I initialised a C = 25 variable.
If C > 5, I want to raise an exception.
Now that you want to use your own customised exception, you’ll have to use the Raise keyword.
While using the Raise keyword, you have to pass your Exception class name.
What is its name? MyException.
Then you have to pass whatever your message is.
Something went wrong.
Now when you run it, it raised your own exception, called our MyException class & also prints our customised message.
This is how you can create Customised User defined Exceptions.
You can use it after inheriting Exception class.
I’ll explain to you what is class & inheritance in further videos.
So what did we see today?
We saw how to create Exceptions…
Suppose I enter Hello here in the integer input, it’ll give an exception.
Then how we created it through User Input, how exception occurs when we take the value from Users,
Later we saw how to handle the exception at runtime.
And lastly we saw Many Exceptions.
We saw the Else part too.
We also saw Finally Block & User Defined Exception.
This is how Exception Handling works.
Our next topic will be Oops. This is when the advanced Python part begins.
See you in the next video.
Thank you.
Share a personalized message with your friends.