Exception Handling: परिभाषा, महत्व और उदाहरण

Exception Handling (Definition)

Exception Handling एक ऐसा OOP concept है, जिसमें program runtime के दौरान होने वाले errors को detect (catch) करके, उन्हें safe तरीके से handle किया जाता है, जिससे पूरा program बिना रुके smoothly चलता रहता है।

C++ में exception handling तीन keywords से चलता है :

  • try
  • catch
  • throw

Why Exception Handling is Important? (क्यों ज़रूरी है?)

  • Prevents Program Crash : Exception Handling errors को catch करके program को crash होने से बचाता है।
  • Improves User Experience : User को helpful messages मिलते हैं जैसे “Invalid Input” या “Try Again Later”.
  • Safely Handles Runtime Errors : Divide by zero, file not found, invalid input जैसे runtime errors को smoothly handle करता है।
  • Isolates Errors : Error सिर्फ उसी block में रुकता है, बाकी program normally चलता रहता है।
  • Easier Debugging : Developer को आसानी से पता चल जाता है कि error कहाँ और क्यों आया।
  • Enhances Security : Login failure या payment error जैसे sensitive operations safely manage होते हैं।
  • Keeps Code Clean : बार-बार if-else लिखने की जरूरत नहीं, structure neat और readable बनता है।
  • Handles Unexpected Situations : Network failure, memory overflow, file corruption जैसी unexpected conditions को भी manage करता है।
  • Improves Maintainability : Error handling अलग block में होने से code flexible और maintainable बनता है।

Syntax

try {
        // risky code
}
catch(type exceptionName) {
      // handle code
}

Exception Keywords in C++

C++ में exception handling तीन main keywords से work करती है :

  1. try
  2. catch
  3. throw

1. try Keyword

try block में वो code लिखा जाता है, जिसमें error आने की संभावना होती है। Program runtime में error detect करने के लिए try block use करता है।

Syntax :

     try {
          // risky code
   }

Example :

try {
      int a = 10, b = 0;
      if(b == 0)
           throw “Division by zero!”;
      cout << a/b;
}

2. throw Keyword

throw keyword exception generate या forward करने के लिए use होता है। जब runtime में कोई error detect होता है, तो उसे throw करके catch block तक भेजा जाता है।

Syntax :

       throw expression; // expression can be int, char, string, or object

Example :

if(b == 0)
       throw “Division by zero!”; // exception thrown

3. catch Keyword

Catch block try block में आने वाले exceptions handle करता है। यह program को crash होने से बचाता है और safe तरीके से execution continue कराता है।

Syntax :

  catch(type variable_name) {
        // handle exception
  }

Example :

catch(const char* msg) {
          cout << “Exception caught: ” << msg;
}

Example (उदाहरण)

#include <iostream>
using namespace std;

int main() {
     int a = 10, b = 0;

    try {
         if(b == 0)
              throw “Division by zero error!”; // Exception thrown
    cout << “Result: ” << a / b;
}
    catch(const char* msg) {
       cout << “Exception caught: ” << msg; // Exception handled
}

  cout << “\nProgram continues after exception.”;
  return 0;
}

Output :

Exception caught : Division by zero error!
Program continues after exception.

Advantages of Exception Handling (लाभ)

  • Prevents Program Crashes : Exceptions को catch करके program को crash होने से बचाया जा सकता है।
  • User-friendly Error Messages : User को meaningful message मिलता है, जैसे: “Invalid Input”, “Try Again Later” आदि।
  • Safely handles runtime errors : Divide by zero, null pointer, file missing जैसी errors को safely manage किया जा सकता है।
  • Increases Code Maintainability : Errors को central point पर handle करने से code modular और clean बनता है।
  • Makes Resource Management Easier : File handles, database connections आदि को safely close किया जा सकता है।
  • Debugging is easier : Exception stack trace से पता चलता है कि error कहाँ और क्यों हुआ।
  • Provides Alternate Flow/Fallback Path : Program crash के बजाय safe alternate path पर continue कर सकता है।

Disadvantages of Exception Handling (हानियाँ)

  • Performance Overhead : Frequent use of try-catch blocks program की speed को थोड़ा slow कर सकता है।
  • Increases Code Complexity : ज्यादा nested try-catch blocks से code confusing और messy हो जाता है।
  • Silent Catch Hides Bugs : Empty catch blocks errors को छुपा सकते हैं, जिससे debugging मुश्किल हो जाती है।
  • Overuse Leads to Misuse : Simple conditions को handle करने के लिए भी exceptions use करना code inefficient और untidy बना देता है।
  • Unchecked vs Checked Exceptions Confusion : Beginners के लिए यह समझना मुश्किल हो जाता है कि कौन सा exception handle करना है और कौन छोड़ना है।
error: Content is protected !!