Let’s see some examples of exceptions;
>>> 10/0
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
10/0
ZeroDivisionError : division by zero
>>> 'silan'+10
Traceback (most recent call last):
File"<pyshell#1>", line 1, in <module>
'silan'+10
TypeError: can only concatenate str (not "int") to str
When the exception arises, program terminates execution. This is called crash.So we have to handle exception.
Let’s see one program how to handle exception:
Here in the above source code the exception handling mechanism consisting try and except keyword. Basically trycontains risky code, that means on which statement, an exception raise that statement we will write in try block. Once a tryblock executes, then control come to the except part and verify the type of exception raised in try block and type of exception reference have taken or not in except part. It should be matched. Then exceptpart executes. Basically except part contain handling code.
So here in try block the exception ZeroDivisionError raised and in except part ZeroDivisionError reference that we have taken. So except part executed , as a result remaining statement executed and finally we got the output. Or we can say program terminated normally.