Type Here to Get Search Results !

Python Exception Handling in Hindi | पाइथन में Error को कैसे संभालें?

0
Python Exception Handling in Hindi | पाइथन में Error को कैसे संभालें?

Python Exception Handling in Hindi | पाइथन में Error को कैसे संभालें?

📘 Chapter 9: एक्सेप्शन हैंडलिंग (Exception Handling)

जब भी हम Python में कोई ऐसा कोड लिखते हैं जिसमें error आ सकता है (जैसे - किसी न मिलने वाली फाइल को खोलना, 0 से भाग देना), तो Python उस error को "exception" कहता है। Exception Handling का मतलब है - इन errors को सही तरीके से पकड़ना और प्रोग्राम को क्रैश होने से बचाना।

🔹 try-except का उपयोग:

try:
    x = 10 / 0
except ZeroDivisionError:
    print("0 से भाग नहीं किया जा सकता!")

🔹 Multiple exceptions को हैंडल करना:

try:
    a = int(input("कोई नंबर डालें: "))
    b = 10 / a
except ValueError:
    print("कृपया केवल नंबर डालें!")
except ZeroDivisionError:
    print("0 से भाग नहीं किया जा सकता!")

🔹 finally block:

finally block हमेशा चलेगा – चाहे exception आए या न आए।

try:
    f = open("data.txt")
    print(f.read())
except:
    print("फाइल नहीं मिली!")
finally:
    print("फाइल ऑपरेशन समाप्त!")

🔹 raise keyword:

हम खुद भी exception फेंक सकते हैं:

age = 15
if age < 18:
    raise Exception("आप वोट नहीं दे सकते!")

📌 इस चैप्टर में आपने सीखा:

  • Exception क्या होता है?
  • try-except block का प्रयोग
  • finally और raise का उपयोग
  • Multiple errors को कैसे handle करें

➡️ अगला चैप्टर:

Chapter 10: पाइथन में Object Oriented Programming (OOP) की शुरुआत

Tags

Post a Comment

0 Comments

Show ad in Posts/Pages