Python Exception Handling Tutorial

An exception is an unexpected or unwanted event that disrupts the normal flow of execution. When an exception raised on a statement in a program, program is abnormally terminated. As a result we are not getting output. So exception arising in a program is a disadvantageous fact.


Example 1

ZeroDivisionError: division by zero


Example 2

IndexError:list index out of range

 

Note: From the above we observed that when an exception raised on a statement, the execution process is stopping, as a result the remaining statements never getting chance to execute which we are telling program is abnormally terminating. So exception is a drawback.

Python provides a mechanism that is exception handling mechanism, which handles exception conveniently.


Exception Handling Mechanism:

This mechanism consists 4 keywords like, try, except, finally, raise.

Try is a block, basically containing risky code. On which statement the exception is arising that statement we are telling risky code.

Once a try block execute, immediately the control will go the except block. Like try, except is a block basically containing handling code. Without except block try block is meaning less. So once try block executed, except block will execute.

Here the most important thing is the type of exception raised on try block and the type of exception alias in except block should match. Otherwise exception will not handle.

Example


finally:

Like try and except, finally is also a block basically it contains clean-up code. That means once a finally block is present in a program that must execute whether an exception is handled or not handled.

Example

try;
x=10/0
except ZeroDivisionError as z;
pass
finally;
print(10/2)
print("Python means SILAN Technology")

Output:
5
Python means SILAN Technology


raise keyword:

Sometimes to fulfill the program requirement, the programmer handover an exception object manually to the control, for this we use raise keyword. That means the raise keyword is used to throw an exception object explicitly. This raise keyword basically used in customized exception.

Example

raise ZeroDivisionError("\ by zero")


Output: ZeroDivisionError: \ by zero


#Customized Exception(User-defined Exception)

When an exception is defined by the user based on the program requirements, is known as customized or user-defined exception.

Example

#Customized Exception(User-defined Exception)
class TooOldException(Exception):
	pass

class TooYoungException(Exception):
	pass

age=int(input("enter the age value"))

if age>60:
	raise TooOldException("sry!!!we cant help u")

elif age<18:
	raise TooYoungException("sry!! u have 2 wait some yrs")

else:
	print("congrats")

Output:
1st Run:
enter the age value29
congrats

2nd Run:

3rd Run:


About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext