Exception Handling in Object-Oriented Programming in Hindi – Definition, Types, and Examples


Exception Handling क्या है?

Object-Oriented Programming (OOP) में Exception Handling एक महत्वपूर्ण प्रक्रिया है, जिसका उपयोग Run-Time Errors को पकड़ने और उनका समाधान करने के लिए किया जाता है। यह Program को Unexpected Errors से बचाने और उसे Crash होने से रोकने में सहायक होती है।

Exception की परिभाषा (Definition of Exception)

Exception एक Error है, जो Program के Execution के दौरान उत्पन्न होती है। Exception Handling का उपयोग इन Errors को Handle करने और Program को सामान्य रूप से चलाने के लिए किया जाता है।

Exception Handling के Key Elements

Python और अन्य Programming Languages में Exception Handling के लिए निम्नलिखित Keywords का उपयोग किया जाता है:

  • try: उस Block को Define करता है, जहां Error उत्पन्न हो सकती है।
  • except: Exception को Handle करने के लिए उपयोग किया जाता है।
  • finally: Cleanup Operations के लिए उपयोग किया जाता है। यह Block हमेशा Execute होता है।
  • raise: Custom Exception उत्पन्न करने के लिए उपयोग किया जाता है।

Example of Exception Handling in Python:

try:
    num1 = int(input("Enter numerator: "))
    num2 = int(input("Enter denominator: "))
    result = num1 / num2
    print(f"Result: {result}")
except ZeroDivisionError:
    print("Error: Division by zero is not allowed.")
except ValueError:
    print("Error: Invalid input. Please enter a valid number.")
finally:
    print("Execution completed.")

Types of Exceptions

Exception मुख्य रूप से दो प्रकार के होते हैं:

  1. Built-in Exceptions: Python और अन्य Programming Languages में पहले से Defined Exceptions जैसे ZeroDivisionError, ValueError, TypeError
  2. Custom Exceptions: Programmer द्वारा Defined Exceptions।

Example of Custom Exception:

class CustomError(Exception):
    def __init__(self, message):
        self.message = message

try:
    age = int(input("Enter your age: "))
    if age < 18:
        raise CustomError("You must be 18 or older.")
    print("You are eligible.")
except CustomError as e:
    print(f"Custom Error: {e.message}")

Advantages of Exception Handling

  • Error Detection: Run-Time Errors को पकड़ने में सहायक।
  • Program Stability: Program को Crash होने से बचाता है।
  • Debugging आसान बनाता है।
  • Custom Error Messages: User-Friendly Error Messages प्रदान करता है।

Applications of Exception Handling

Exception Handling का उपयोग विभिन्न क्षेत्रों में किया जाता है:

  1. Real-Time Systems
  2. Web Applications
  3. Database Management Systems
  4. Game Development
  5. Financial Applications

Conclusion

Exception Handling Object-Oriented Programming का एक महत्वपूर्ण हिस्सा है। यह Program को Unexpected Errors से बचाने और उसे Smoothly Execute करने में सहायक होता है। इसकी समझ Complex Applications के Development में उपयोगी होती है।

Related Post