Python Exceptions and Handling (Python Exceptions and Handling क्या हैं ?)

codeinhindi

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

ExceptionsDescription
ArithmeticErrormath या numeric calculations के सम्बंधित error का ये base class होता है |
AssertionErrorassert 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 होता है |
FloatingPointErrorfloating-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 होता है |
KeyErrordictionary में जब 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 होता है |
StandardErrorStopIteration और 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 होता है |
UnicodeEncodeErrorencoding के वक्त जब unicode से सम्बंधित error आता है तब ये exception raise होता है |
UnicodeErrorunicode से सम्बंधित जब encoding या decoding error आता है तब ये exception raise होता है |
UnicodeTranslateErrortranslating के वक्त जब 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 ?

Exceptions ये Python में Errors के प्रकार होते है | Exception के अलग-अलग प्रकार पड़ते है | हर अलग-अलग error के लिए अलग-अलग exception होता है |

जब Python के program पर error occured होता है तब कोई ना कोई exception raise होता है |

जब program पर किसी statement के लिए exception raise होता है तो उस statement के आगे की script exeception की वजह से execute नहीं होती है |

इन scripts को executes करने के लिए error/exeception को handle करना पड़ता है |

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


अगर आपको यह पोस्ट 📑 (Python Exceptions) पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !
Maxon

Hello there! I'm Maxon, a dedicated UI/UX designer on a mission to transform digital experiences into intuitive, user-centric journeys. With a keen eye for detail and a passion for crafting aesthetically pleasing interfaces, I strive to create designs that not only look stunning but also enhance usability and functionality.

Post a Comment

Previous Post Next Post