Just imagine, you are asked to enter your gmail Id and Password to login to your Gmail account.But, by mistake you have entered a wrong password. And what if, Gmail exits without giving you a second chance to enter the Password? - It is not acceptable.
Same thing applies for any application. We are allowed to make some sort of mistakes. So that we get a second chance to rectify that. Such mistakes in Python are called as Exceptions.
So, the Exceptions should be handled/caught at runtime, and also the entire application should not close due to that Exception.
It is same as your Gmail account doesn't close when someone types a wrong password and at the same time Gmail needs to keep a track on how many times the user is entering a wrong Password.
Say for example, you have written a program to divide two numbers then add them. But by mistake, you tried to divide the number by zero.
Let us see what happens in the below example.
x = 3 y = 0 z = x / y print("The divided result is ",z) k = x + y print("The added result is ",k)
So, if you see the above example, we have declared tow variables 'x' and 'y' and initialised '3' and '0' to them.
Then we tried dividing them,
And since, we tried to divide '3' by '0'. It ended up with an error.
That could be ok. But the problem is, the addition operation was not performed for the above error.
And the program got halted. Which is not acceptable.
And Python provides a solution to the above problem, with something called as a 'try' - 'except' block.
So, in the 'try' block, you can place the code if you suspect it might throw error.
And in the 'except' block, you can handle the error. Only if there is an error, the 'except' block executes.
Now, all you need to do is, place the division operation inside the blocks of 'try'. And if you want to display some message for that error, you can place the message in the 'except' block.
Now, let's modify the above code using 'try' - 'except' block.
x = 3 y = 0 try: z = x / y print("The divided result is ",z) except: print("The division couldn't be performed due to an error.") k = x + y print("The added result is ",k)
Now, if you see the above output, it proceeded without any error. So, let us see how it worked.
So, in the above code, we have placed the division operation inside the 'try' block.
try: z = x / y print("The divided result is ",z)
And as usual an error occurred because we wanted to divide the number by '0'.
And luckily, the 'try' block, handled the error. And the control comes to the 'except' block.
except: print("The division couldn't be performed due to an error.")
And prints the output.
And most importantly, the addition operation didn't get halted. And the below line executes,adding two numbers.
k = x + y print("The added result is ",k)
But let's say you have missed to assign value to a variable.
y = 0 z = x / y print("The divided result is ",z)
In the above case, you forgot the define the value of 'x'. And you ended up with the below Error.
So, there could be two types of errors. i.e. A 'ZeroDivisionError' and a 'NameError'.
To handle multiple errors, Python provides multiple 'except' blocks, with one 'try' block.
y = 0 try: z = x / y print("The divided result is ",z) except NameError: print("The division couldn't be performed as the variable is not defined.") except ZeroDivisionError: print("The division couldn't be performed as the number was divided using zero.")
Now, if you see the above output, the error message is more specific.
i.e. In the above program, we have forgot to define the value of 'x'. And in Python it is called as 'NameError'.
So, when the in the 'try' block,
try: z = x / y print("The divided result is ",z)
When Python tried to divide two numbers,
It finds the value of 'x' is missing.
So, it goes to the 'except' block with the name of the error as 'NameError'.
And it finds one.
except NameError: print("The division couldn't be performed as the variable is not defined.")
So, it prints the output,
Similarly, when there is a division error. The 'except' block with 'ZeroDivisionError' executes.
x = 3 y = 0 try: z = x / y print("The divided result is ",z) except NameError: print("The division couldn't be performed as the variable is not defined.") except ZeroDivisionError: print("The division couldn't be performed as the number was divided using zero.")
Now, let us say, if there is no error. You want to display a message that there is no error.
And Python provides an 'else' block for that.
If there is an error the 'except' block is executed and if there is no error 'else' block gets executed.
x = 3 y = 1 try: z = x / y print("The divided result is ",z) except: print("The division couldn't be performed due to an error.") else: print("There is no error")
So, in the above program, there is no error. So, the 'else' block got executed.
else: print("There is no error")
Printing, 'There is no error' along with the divided result.
Now, let us say the below program has a division error.
x = 3 y = 0 try: z = x / y print("The divided result is ",z) except: print("The division couldn't be performed due to an error.") else: print("There is no error")
So, in the above program the 'else' block did not get executed as there was an error.
Now, let us say, irrespective of any error. You want to display a message.
And Python provides a 'finally' block for that.
The 'finally' block gets executed irrespective of any error.
Let us see the below example, where there is no error.
x = 3 y = 1 try: z = x / y print("The divided result is ",z) except: print("The division couldn't be performed due to an error.") finally: print("try block ended")
So, in the above program, there is no error. And the 'finally' block got executed.
finally: print("try block ended")
Now, let us say the below program has a division error.
x = 3 y = 0 try: z = x / y print("The divided result is ",z) except: print("The division couldn't be performed due to an error.") finally: print("try block ended")
So, the 'finally' block gets executed, whether there is an error or not.