Exception Handling
Exception Handling वह प्रक्रिया है जिसमें प्रोग्राम के चलने के दौरान आने वाली Runtime Errors को संभाला जाता है ताकि प्रोग्राम अचानक बंद न हो और सही तरीके से चलता रहे।
Exception Handling की आवश्यकता (Need)
| Reason | Description |
|---|---|
| Program Crash रोकता है | Program अचानक बंद नहीं होता। |
| Error Handle करता है | Runtime Error को संभालता है। |
| User Friendly | User को सही Message दिखाता है। |
| Debugging आसान | Error आसानी से पता चलती है। |
| Program Continue रहता है | Error आने पर भी बाकी Program चल सकता है। |
| Reliability बढ़ती है | Program अधिक भरोसेमंद बनता है। |
| Security बेहतर होती है | Unexpected Error से बचाता है। |
Exception Handling Syntax :
try:
# Error वाला Code
except:
# Error Handle करने वाला Code
Exception Handling Advantages
| 1 | Program Crash होने से बचता है। |
| 2 | Runtime Errors को Handle करता है। |
| 3 | User Friendly Message दिखाता है। |
| 4 | Debugging आसान बनाता है। |
| 5 | Program Reliable बनता है। |
| 6 | Program का Execution जारी रहता है। |
| 7 | Code Maintain करना आसान होता है। |
Exception Handling Disadvantages
| 1 | Program थोड़ा Slow हो सकता है। |
| 2 | Code लंबा हो जाता है। |
| 3 | अधिक try-except से Readability कम हो सकती है। |
| 4 | गलत Exception Handle करने से Error छिप सकती है। |
| 5 | Debugging कठिन हो सकती है यदि Exception दबा दी जाए। |
1. Try Block
Try Block का use जिस Code में Error आने की संभावना हो उसे try Block में लिखा जाता है।
Example :
try:
print(10/0)
2. Except Block
अगर try ब्लॉक में कोई गलती होती है, तो यह हिस्सा उसे पकड़ता है और प्रोग्राम को क्रैश होने से बचाता है। यहाँ आप यह तय करते हैं कि गलती होने पर क्या करना है।
Example
try:
print(10/0)
except:
print("Error आया है")
3. finally
यह ब्लॉक हमेशा चलता है, चाहे गलती हो या न हो। इसका उपयोग अक्सर संसाधनों (Resources) को बंद करने के लिए किया जाता है (जैसे फाइल बंद करना)।
Example :
try:
pass
except:
pass
else:
pass
4. throw / raise
इसका उपयोग जानबूझकर खुद की बनाई हुई गलती (Custom Error) पैदा करने के लिए किया जाता है।
Example :
raise ExceptionType("Error Message")
5. Finally Block
finally Block हमेशा Execute होता है, चाहे Error आए या न आए।
Example :
try:
print(10/0)
except:
print("Error")
finally:
print("Program End")
Common Python Exception
| Exception | Definition | Syntax | Example | Solution | Uses |
|---|---|---|---|---|---|
| Zero DivisionError | जब किसी संख्या को 0 से Divide किया जाता है। | num1 / num2 (यदि num2 = 0) | python\nprint(10/0)\n | Divisor को पहले Check करें या try-except का उपयोग करें। | Calculator, Mathematical Programs |
| ValueError | सही Data Type हो लेकिन Value गलत हो। | int(value) | python\nnum = int("abc")\n | Input Validation करें और try-except का उपयोग करें। | User Input Validation |
| TypeError | अलग-अलग Data Types पर गलत Operation करने पर। | value1 + value2 | python\nprint(10 + "20")\n | Data Type Convert करें (int(), str())। | Data Processing |
| IndexError | List या Tuple के बाहर Index Access करने पर। | list[index] | python\nlist1=[10,20,30]\nprint(list1[5])\n | Index की सीमा len() से Check करें। | List Handling |
| KeyError | Dictionary में मौजूद न होने वाली Key Access करने पर। | dict[key] | python\nstudent={"name":"Rahul"}\nprint(student["marks"])\n | get() Method या try-except का उपयोग करें। | Dictionary, JSON Handling |
| NameError | जब Variable Define न किया गया हो। | print(variable) | python\nprint(total)\n | Variable पहले Declare करें। | Debugging |
| FileNotFoundError | जब File मौजूद न हो। | open("file","r") | python\nopen("student.txt","r")\n | सही File Path दें या try-except का उपयोग करें। | File Handling |
| ImportError | जब Module या Function Import न हो पाए। | from module import function | python\nfrom math import abc\n | सही Module या Function Import करें। | Library Management |
| AttributeError | जब Object में उपलब्ध न होने वाला Method उपयोग किया जाए। | object.method() | python\nname="Python"\nname.append("A")\n | सही Method का उपयोग करें। | OOP (Object-Oriented Programming) |
| OverflowError | जब Numeric Value बहुत बड़ी हो जाए। | math.exp(number) | python\nimport math\nmath.exp(1000)\n | छोटी Value का उपयोग करें या Exception Handle करें। | Scientific Calculations |
- इसे भी पढ़े : Python Operators in Hindi
- इसे भी पढ़े : Python Loops in Hindi
- इसे भी पढ़े : Python Statement in Hindi
- Python Full Course Roadmap 2026Python Introduction↓Python Installation & Setup↓Python Basics↓Variables & Data Types↓Operators↓Conditional Statements↓Loops↓Functions↓Data Structures↓File Handling↓Exception Handling↓Object Oriented Programming (OOP)↓Modules & Libraries↓Advanced Python↓Database Connectivity↓Web Development↓Data Science & AI↓Automation & Projects↓Python Developer
- What is Python in Hindi (Basic to advanced)Introduction and History Introduction for Python Python History Python Features Python Applications Applications Uses 1. Artificial Intelligence (AI) and Machine Learning Python का उपयोग AI models, Machine Learning algorithms, Chatbots, Face Recognition, Voice Assistant और Prediction systems बनाने के लिए किया जाता है। 2. Data Science and Data Analysis Python से बड़े datasets को analyze… Read more: What is Python in Hindi (Basic to advanced)
- Python Keywords in Hindi (easy to understand )Python Keywords में ऐसे special word होते हैं जिनका Python भाषा में पहले से एक Predefined meaning होता है। Python Interpreter इन words को पहचानता है और इनके अनुसार कार्य करता है। Example : No. Keyword हिंदी में अर्थ Uses (उपयोग) Syntax 1 False गलत False keywords का use Boolean False value के लिए किया… Read more: Python Keywords in Hindi (easy to understand )
- Python Variables in Hindi (Easy to understand)what is Variable Assigning Value to variable Source Code : Output : 5 Hello [2, 5, 9] Changing Variable’s Values Source Code : Output : 5Hello[4, 5, 8] Assigning Single Value to Multiple Variables Source Code : Output : HelloHelloHelloHello Assigning Value to Variable according to order Source Code : Output : 1H[1, 2] Variable… Read more: Python Variables in Hindi (Easy to understand)
- Python Data Types in Hindi (easy to understand)Python में Data Type यह बताता है कि किसी variable में किस प्रकार का data store है। Python में data type को manually declare नहीं करना पड़ता, Python interpreter value देखकर data type पहचान लेता है। Example : Number Python में Number Data Types का उपयोग संख्याओं (Numbers) को store और process करने के लिए… Read more: Python Data Types in Hindi (easy to understand)

