
Python Exceptions and Handling in Hindi
नमस्कार दोस्तो आज इस आर्टिकल में जानेंगे कि Python Exceptions and Handling क्या हैं ?
Exceptions
Python में जब error occurred होता है तब वहा पर कोई ना कोई exception raise होता है |
Exceptions ये error का ही एक प्रकार है | Python में कुछ built-in Exceptions होते है | एक exceptions के जरिये Python के errors को handle किया जाता है |
Normal Example for Exception
Example पर 'a' ये variable defined नहीं किया गया है | इसीलिए 'NameError' ये exception raise हुआ है |
Source Code :print(a)
Output : print(a)
NameError: name 'a' is not defined
निचे कुछ महत्वपूर्ण Exceptions दिए गए है |
Important Exceptions in Python
Exceptions | Description |
---|---|
ArithmeticError | math या numeric calculations के सम्बंधित error का ये base class होता है | |
AssertionError | assert statment जब नाकाम होता है तब ये exception raise होता है | |
AttributeError | अगर attribute reference या assignment fail होता है तो ये exception raise होता है | |
EOFError | जब 'input()' ये built-in function बिना data read किये जब end-of-file(EOF) इस condition पर पहुंचता है तब ये exception raise होता है | |
EnvironmentError | जब Python Environment के बाहर से कुछ पाया जाता है तो ये उन सभी exceptions का base class होता है | |
Exception | सभी exceptions का ये base class होता है | |
FloatingPointError | floating-point calculation जब नाकाम होता तो ये exception raise होता है | |
GeneratorExit | जब generator का close() method call किया जाता है तब ये exception raise होता है | |
IOError | जब input or output operations नाकाम होते है तब ये exception raise होता है | |
ImportError | जब import किया हुआ module नहीं मिलता तब ये exception raise होता है | |
IndentationError | जब indentation गलत होता है तो तब ये exception raise होता है | ये SyntaxError का subclass होता है | |
IndexError | जब index out of range दिया जाता है तब ये exception raise होता है | |
KeyError | dictionary में जब key found नहीं होती है तब ये exception raise होता है | |
KeyboardInterrupt | जब program execution के वक्त कुछ बाधा आती है तब ये exception raise होता है | ख़ास करके जब execution के वक्त Ctrl+c को दबाया जाता है | |
LookupError | ये सभी lookup errors का base class होता है | |
MemoryError | जब operation out of memory हो जाता है तब ये exception raise होता है | |
NameError | जब local या global scope पर variable found नहीं होता है तब ये exception raise होता है | |
NotImplementedError | ये exception abstract methods द्वारा raise होता है | |
OSError | ये operating-system से सम्बंधित exception होता है | |
OverflowError | जब numeric calculations हद से ज्यादा बड़े होते है तब ये exception raise होता है | |
RuntimeError | जब error किसी भी category में नहीं होता है तो ये exception raise होता है | |
StandardError | StopIteration और Systemexit के सिवाय ये सभी exceptions का base class होता है | |
StopIteration | जब next() function के iterator किसी भी object का वर्णन नहीं करता है तब ये exception raise होता है | |
SyntaxError | जब Python के syntax में error होता है तब ये exception raise होता है | |
SystemError | जब interpreter द्वारा internal problem found होता है तब ये exception raise होता है | |
SystemExit | जब sys.exit() द्वारा interpreter को बंद किया जाता है तब ये exception raise होता है | |
TabError | जब indentation पर अतिरिक्त tabs और spaces दिए जाते है तब ये exception raise होता है | |
TypeError | जब जरुरत के हिसाब से invalid data type की value दी जाती है तब ये exception raise होता है | |
UnboundLocalError | अगर function के local varaible या method को access किया जाता है और उनकी value वहापर assign नहीं होती है तो ये exception raise होता है | |
UnicodeEncodeError | encoding के वक्त जब unicode से सम्बंधित error आता है तब ये exception raise होता है | |
UnicodeError | unicode से सम्बंधित जब encoding या decoding error आता है तब ये exception raise होता है | |
UnicodeTranslateError | translating के वक्त जब unicode से सम्बंधित error आता है तब ये exception raise होता है | |
ValueError | जब in-built function पर valid data type देना जरुरी होता है लेकिन वहा पर valid data type की value नहीं दी जाती है तो ये exception raise होता है | |
ZeroDivisonError | जब division का दूसरा operand '0' होता है तब ये exception raise होता है | |
Python - Exception Handling
What is Exception ?
For Example,
Example पर तीन variables को defined करके print किया गया है | लेकिन जहा पर print statements है वहा पर अतिरिक्त undefined 'd' variable को print करने की अनुमति दी गयी है |
'd' variable defined न होने की वजह से exeception raise होता है | 'd' के print statement से पहले का print statement execute होता है लेकिन उसके बाद के statements print नहीं हो पाते है |
उन अगले statements को print करने के लिए exception को handle करना पड़ता है |
a = 10
b = 20
c = 30
print(a)
print(d) #exception raised
print(b)
print(c)
Output :10
print(d)
NameError: name 'd' is not defined
Example for Exception Handling
Example पर Exception को handle किया गया है |
a = 10
b = 20
c = 30
print(a)
try:
print(d)
except(Exception):
print("Variable is not defined")
print(b)
print(c)
Output :10
Variable is not defined
20
30
try_except Statement(Exception Handling) in Python
Python में error को handle करना हो तो 'try_except' statement का इस्तेमाल करना पड़ता है | Python में जिस code पर error occured होने की संभावना होती है वो code 'try' block पर आता है और except block पर exception/error को handle करने के लिए कुछ code दिया जाता है |
Syntax for 'try_except' Statement
try:
try_block_code
except Exception1:
exception handling code 1
except Exception2:
exception handling code 2
Syntax में देखे तो एक try block के लिए एक या एक से अधिक except clauses लिए जा सकते है |
Example for 'try_except' Statement
Example में try block पर जो code दिया है वो 'ZeroDivisionError' exception raise करता है इसीलिए ZeroDivisionError का ही except clause execute होगा |
Source Code :try:
a = 10
a/0
except NameError:
print("'a' is not defined")
except ZeroDivisionError:
print("Divided By Zero Error")
Output :Divided By Zero Error
'try_except' with 'else' Statement(Exception Handling) in Python
Syntax for 'try_except' Statement
try:
try_block_code
except Exception1:
exception handling code 1
except Exception2,Exception3:
exception handling code 2
else:
else_block_code
Syntax में देखे तो एक try block के लिए एक या एक से अधिक except clauses लिए जा सकते है |
जब try block code में दिया हुआ code कोई भी exception raise नहीं करता है तो else block code execute होता है |
Example में try block code कोई exception raised नहीं करता है इसीलिए else block code execute होगा |
Source Code :try:
a = 10
a/4
except NameError:
print("'a' is not defined")
except ZeroDivisionError:
print("Divided By Zero Error")
else:
print("No Exception raised")
Output :No Exception raised
except clause with More than one Exception
Syntax :try:
try_block_code
except Exception1:
exception handling code 1
except (Exception2, Exception3):
exception handling code 2
Syntax में देखे तो एक try block के लिए एक या एक से अधिक except clauses लिए जा सकते है और हर except clause में एक या एक से अधिक exceptions दिए जा सकते है | लेकिन उन multiple exceptions को parenthesis(()) में close करना पड़ता है |
Example :
try:
b = a/4
print("Value of b is",b)
except ZeroDivisionError:
print("'a' is not defined")
except (TypeError,NameError):
print("Something is wrong")
else:
print("No Exception raised")
Output :Something is wrong
try_except_finally Block(Exception Handling) in Python
Syntax :try:
try_block_code
except Exception(s):
exception_handling_code
finally:
always_executed_code
try block का code जब कोई execption raised करता है और उसे handle नहीं किया जाता है तो finally block का code पहले execute होता है |
try block का code जब कोई execption raised करता है और उसे handle किया जाता है तो except block का code पहले execute होता है और बाद में finally block का code execute होता है |
अगर exception raised होता है या नहीं होता है finally block का code हमेशा execute होता है |
Source Code :try:
"5" + 4
except TypeError:
print("Value must be a Number")
finally:
print("Alway executed")
Output :Value must be a Number
Alway executed
Raising Exceptions
Exception user द्वारा raise करने 'raise statement' का इस्तेमाल किया जाता है |
raise Statement program में कोई भी exception raised करने के लिए मजबूर कर देता है |
Syntax :raise exception('message') #argument is optional
Source Code :raise TypeError("HI")
Output : raise TypeError("HI")
TypeError: HI
Another Example for Raising Exception
Source Code :try:
raise ValueError('ValueError raised forcely')
except Exception as e:
print ('Error :',e)
Output :Error : ValueError raised forcely