Exception Handling of Python is a mechanism to handle unexpected runtime errors only (the last one), but not any other types of errors discussed above. Exception can be defined as an unwanted situation during the execution of the program. Exception causes the abnormal termination of the program. This situation is not suitable for the following examples real-time scenarios
1. ATM program is closed due to wrong password entry by a user.
2. A network application is closed due to some mistake done by the user on a particular node.
Handling of exception is the concept of avoiding abnormal termination of the program due to exception so that the program would continue its execution by taking necessary steps regarding exception. The handling of exception is not providing solution for the exception but it is providing an alternative way to run the rest of the program which is not dependent on the code in which exception occurred.
#Example code with run-time error
x=10
y=0
print(“Division Operation”)
print(x/y)
print(“rest of the code”)
The output of the above code would be like below:
Division Operation
Traceback (most recent call last):
File “D:\Python Programs\practice.py”, line 4, in <module>
print(x/y)
ZeroDivisionError: division by zero
The exception raised in the above code is divisible by zero in the line “print(x/y)”. You can observe there is an output statement that is displaying a string “print(“rest of the code”) is not executing even though it is not at all dependent on the code line in which exception occurred.
This is also known as abnormal termination or automatic exception handling handed over to PVM.
#same program with exception handling
x=10
y=0
print(“Division Operation”)
try:
print(x/y)
except:
print(“Division by zero Error”)
print(“rest of the code”)
The output of the above program is :
Division Operation
Division by zero Error
rest of the code
The difference you can get from the second example is, it continues with the printing of the string “rest of the code…” by showing respecting exception message. This is known as manual exception handling due to which the program would be terminated normally.
Manual Exception Handling in Python is done by try…except pair. The risky code (i.e. the code where there may be chance of exception raising) is kept under the control of try block. The try block informs its following except block about the exception raised inside try block. The except block is responsible to handle the exception raised inside try block.
#Example
try:
print(x)
except:
print(“Exception raised”)
print(“rest of the code”)
Mutiple except blocks
One try block may be followed by multiple except blocks. For each different exception raised inside try block there must be a specific except block can be defined with the name of corresponding errorname. The except blocks must be in the order of most specific to most general.
y=0
try:
print(x) #NameError
x=10
print(x/y) #ZeroDivisionError
except NameError:
print(“variable x is not defined”)
except ZeroDivsionError:
print(“Division by zero is not possible”)
except:
print(“exception raised is not known”)
print(“rest of the code”)
Points regarding Multiple except blocks:
Only one out of all except blocks will be executed
Once an exception raised inside the try block, there will be no chance of execution for the rest of the code inside try block, following exception caused statement.
The else block followed by try…except
The else block can be used with try…except blocks to specify the special code that is to be executed when no exception raised inside try block.
#Example showing using else with try…block
try:
print(“Python”)
except:
print(“Exception raised”)
else:
print(“Everything is fine”)
Finally
The finally is the block which is also used with try…except pair. The finally will always be executed regardless of exception. In general, the cleanup code will be put inside finally block. A try block may have many except but only one finally block. Try without except or finally is invalid.
#example showing use of finally block
try:
print(“hi”)
except:
print(“Exception raised”)
finally:
print(“finally block always executed”)
print(“rest of the code”)