Exception are errors that occur at runtime .
In other words , if our program encounters an abnormal situation during it’s execution it raises an exception.
For example,the statement a=10/0
will generate an exception because Python has no way to solve division by 0
Whenever an exception occurs , Python does 2 things :
1. It immediately terminates the code
2. It displays the error message related to the exception in a technical way
Both the steps taken by Python cannot be considered user friendly because
Even if a statement generates exception , still other parts of the program must get a chance to run
The error message must be simpler for the user to understand.
Example:
a=int(input("Enter first no:"))
b=int(input("Enter second no:"))
c=a/b
print("Div is",c)
d=a+b
print("Sum is",d)
Output:
Enter first no: 10
Enter second no: 5
Div is 2.0
Sum is 15
Output
Enter first no: 10
Enter second no: 0
Traceback (most recent call last):
File "except1.py", line 3, in <module>
c=a/b
ZeroDivisionError: division by zero
If we want our program to behave normally , even if an exception occurs , then we will have to apply Exception Handling