Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 Beginner  ·  Lesson 18

Exception Handling

Exception क्या है?

Exception वह error है जो program चलते समय आती है (जैसे zero से भाग)। handle न करने पर program crash हो जाता है। Exception handling आपको error पकड़कर शालीनता से जवाब देने देता है।

try / except

Python – tryexcept
try:
    # risky code
    risky_operation()
except SomeError:
    # वह error होने पर चलता है
    handle_it()

Python try block आज़माता है। error आने पर crash के बजाय matching except पर कूद जाता है।

Common Errors

Errorकारण
ZeroDivisionError0 से भाग
ValueErrorint("abc") — गलत type की value
TypeError"5" + 5 — types मिलाना
FileNotFoundErrorन मौजूद file खोलना
IndexErrorList index out of range

Program 1: Safe Division

Python – divide.py
a = int(input("Numerator: "))
b = int(input("Denominator: "))
try:
    print("Result:", a / b)
except ZeroDivisionError:
    print("Error: cannot divide by zero")
Numerator: 10 Denominator: 0 Error: cannot divide by zero

try के बिना 0 से भाग program crash कर देता। अब यह friendly message print करता है।

Program 2: Safe Number Input

Python – safeinput.py
try:
    age = int(input("Enter your age: "))
    print("Next year you will be", age + 1)
except ValueError:
    print("Please enter a valid number")
Enter your age: hello Please enter a valid number

User number की जगह text type करे तो int() ValueError देता है, जिसे हम पकड़ते हैं।

Program 3: कई except + finally

Python – multi.py
try:
    nums = [10, 20, 30]
    i = int(input("Index: "))
    print(nums[i])
except ValueError:
    print("Index must be a number")
except IndexError:
    print("Index out of range")
finally:
    print("Done checking")
Index: 5 Index out of range Done checking
  • हर except अलग error type handle करता है।
  • finally हमेशा चलता है — error हो या न हो — cleanup के लिए अच्छा।

अपना Error Raise करना

Input गलत होने पर खुद error देने के लिए raise use करें।

Python – raise.py
def set_age(age):
    if age < 0:
        raise ValueError("Age cannot be negative")
    print("Age set to", age)

try:
    set_age(-5)
except ValueError as e:
    print("Caught:", e)
Caught: Age cannot be negative

सामान्य गलतियाँ

  • bare except: से हर error पकड़ना — असली bugs छुपा देता है।
  • एक try block में बहुत ज़्यादा code डालना।
  • यह भूलना कि finally हर हाल में चलता है।

Practice Tasks

  1. दो numbers पूछकर safely divide करें।
  2. तब तक number पूछते रहें जब तक user valid न दे।
  3. File खोलकर FileNotFoundError अच्छे से handle करें।
  4. एक function लिखें जो negative marks पर error raise करे।

सारांश

  • Exceptions runtime errors हैं; handling crashes रोकता है।
  • try risky code चलाता है; except specific errors पकड़ता है।
  • finally हमेशा चलता है; raise अपना error बनाता है।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

try और except का क्या उपयोग है?
जो code fail हो सकता है वह try में जाता है; except error पकड़ता है ताकि program crash होने की जगह अच्छा message दिखाए।
finally block कब चलता है?
हमेशा – error आए या न आए। इसे files बंद करने जैसे clean-up के लिए इस्तेमाल करते हैं।
क्या बिना error type के except: लिखना चाहिए?
इससे बचें। ValueError या FileNotFoundError जैसे specific errors पकड़ें ताकि असली bugs छिपें नहीं।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicPython में File Handling आपकी प्रगति: 18 / 66 (27%) · Lesson 19 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें