Python – Exceptions
- What is Exception ?
Program के execution के समय अगर कोई error detect होती है तो उसे हम Exception कहते है |
( Errors detected during execution are called Exception. )
Simple Example for Exception :
print (a)
print (a)
NameError: name 'a' is not defined
दिए गए उदहारण मे हमने variable ‘a’ को define नहीं किया जिस कारण NameError आया | यह एक्सेप्शन raise हुआ है |
Python : Some Important Exceptions
Exception NameError :
जब कोई वेरिएबल define नहीं किया जाता या वेरिएबल found नहीं होता तो यहाँ पे Exceptions raise होता है |
For Example :
print (information)
# output
print (information)
NameError: name 'information' is not defined
दिए गए उदहारण मे हमने variable 'information' को define नहीं किया
जिस कारण NameError आया | यहाँ पे Exceptions raise होता है |
Exception ZeroDivisionError:
For Example :
a=5
print (a/0)
# Output
print (a/0)
ZeroDivisionError: division by zero
Hacker Rank : Exception Handling Task

Solution :
T = int(input("enter number of test cases : "))
for i in range(T):
try:
a, b = map(int, input("enter 1st and 2nd number : ").split())
z = int(a / b)
print(z)
except ZeroDivisionError:
print("Error Code: integer division or modulo by zero")
except ValueError as e:
print("Error Code:", e)
OUTPUT :
enter number of test cases : 3
enter 1st and 2nd number : 1 0
Error Code: integer division or modulo by zero
enter 1st and 2nd number : 2 $
Error Code: invalid literal for int() with base 10: '$'
enter 1st and 2nd number : 3 1
3