Exception Handling in Hindi

Exception Handling in java in Hindi | जावा में एक्सेपसन हैंडलिंग हिंदी में :


 एक्सेप्शन (Exception) क्या होता है?

जब कोई एरर (Error) प्रोग्राम के रनटाइम (Run-time) में आता है, तो उसे Exception कहते हैं।

"Exception" एक unexpected (अप्रत्याशित) समस्या है, जो प्रोग्राम के execution को रोक सकती है।

रियल लाइफ उदाहरण:
मान लीजिए कि आप गाड़ी चला रहे हैं और अचानक रास्ते में गड्ढा आ जाता है, तो आप ब्रेक लगाते हैं या गाड़ी मोड़ लेते हैं।

गड्ढा (Error) → Exception
ब्रेक लगाना या गाड़ी मोड़ना (Handle करना) → Exception Handling

जावा में एक्सेप्शन क्यों आता है?

कुछ आम कारण, जिनसे Exception आ सकते हैं:

  • किसी संख्या को 0 से भाग देना (Divide by Zero)
  • Array के बाहर index एक्सेस करना (Array Index Out of Bound)
  • null value को एक्सेस करना (Null Pointer Exception)
  • गलत टाइप का डेटा डालना (Input Mismatch Exception)
  • फाइल न मिलना (File Not Found Exception)

Example (Divide by Zero Exception):

public class Example {
    public static void main(String[] args) {
        int a = 10, b = 0;
        int result = a / b; // यहाँ Error आएगा (ArithmeticException)
        System.out.println("Result: " + result);
    }
}

Error:
Exception in thread "main" java.lang.ArithmeticException: / by zero

 Exception Handling क्यों ज़रूरी है?

  • अगर Exception को handle नहीं किया जाए, तो:
  •  प्रोग्राम क्रैश (crash) हो सकता है।
  • डेटा लॉस हो सकता है।
  • यूज़र एक्सपीरियंस खराब हो सकता है।
  • इसलिए, Exception Handling का उपयोग किया जाता है, ताकि प्रोग्राम स्मूथली चले और एरर को सही से हैंडल किया जाए।

 जावा में Exception Handling कैसे करें?
जावा में Exception Handling के लिए 4 कीवर्ड होते हैं:

Try-Catch Block (Exception Handling Example)

Basic Example (Divide by Zero Exception को Handle करना)

public class TryCatchExample {
    public static void main(String[] args) {
        try {
            int a = 10, b = 0;
            int result = a / b; // यहाँ Exception आएगा
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {
            System.out.println("Error: किसी संख्या को 0 से विभाजित नहीं कर सकते!");
        }
        System.out.println("प्रोग्राम नॉर्मली चल रहा है।");
    }
}
 Output:

Error: किसी संख्या को 0 से विभाजित नहीं कर सकते!
प्रोग्राम नॉर्मली चल रहा है।

  • Try Block: Error आने वाले कोड को try block में रखा जाता है।
  • Catch Block: Error आने पर यह catch block execute होता है।
  • Catch में Exception Object (e) होता है, जिससे error message लिया जा सकता है।
Multiple Catch Blocks (एक से ज्यादा Exception Handle करना)

अगर हमें अलग-अलग Exceptions को अलग तरीके से Handle करना है, तो Multiple Catch Blocks का उपयोग कर सकते हैं।

Example:

public class MultipleCatchExample {
    public static void main(String[] args) {
        try {
            int arr[] = {10, 20, 30};
            System.out.println(arr[5]); // ArrayIndexOutOfBoundsException
            
            int a = 10 / 0; // ArithmeticException
        } 
        catch (ArithmeticException e) {
            System.out.println("Error: 0 से भाग नहीं कर सकते!");
        } 
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Error: Array की सीमा से बाहर!");
        } 
        catch (Exception e) {
            System.out.println("Error: कुछ गलत हुआ!");
        }
    }
}

Output:

Error: Array की सीमा से बाहर!
 कैच ब्लॉक्स क्रम में होते हैं, और सही Exception को पकड़ते हैं।

 Finally Block (हमेशा Execute होने वाला कोड)

Finally Block हमेशा execute होता है, चाहे Exception आए या न आए।

Example:

public class FinallyExample {
    public static void main(String[] args) {
        try {
            int a = 10 / 0;
        } catch (ArithmeticException e) {
            System.out.println("Error: 0 से भाग नहीं कर सकते!");
        } finally {
            System.out.println("यह कोड हमेशा चलेगा।");
        }
    }
}
Output:

Error: 0 से भाग नहीं कर सकते!
यह कोड हमेशा चलेगा।
Finally Block फाइल बंद करने, कनेक्शन बंद करने आदि के लिए इस्तेमाल होता है।

Throw और Throws (कस्टम Exception फेंकना)

Throw का उपयोग Exception को मैन्युअली फेंकने (manually throw करने) के लिए होता है।

Example:

public class ThrowExample {
    static void checkAge(int age) {
        if (age < 18) {
            throw new ArithmeticException("अभी आप वोट नहीं कर सकते!");
        } else {
            System.out.println("आप वोट कर सकते हैं।");
        }
    }

    public static void main(String[] args) {
        checkAge(16); // यहाँ Exception फेंका जाएगा
    }
}
 Output:

Exception in thread "main" java.lang.ArithmeticException: अभी आप वोट नहीं कर सकते!
Throw का उपयोग हम अपने कस्टम Exception को फेंकने के लिए कर सकते हैं।

Custom Exception (अपने खुद के Exception बनाना)

अगर हमें कोई खास तरह का Exception बनाना हो, तो हम Custom Exception Class बना सकते हैं।

Example:

class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}

public class CustomExceptionExample {
    static void checkNumber(int num) throws MyException {
        if (num < 0) {
            throw new MyException("Negative संख्या स्वीकार्य नहीं है!");
        }
    }

    public static void main(String[] args) {
        try {
            checkNumber(-5);
        } catch (MyException e) {
            System.out.println("Caught Exception: " + e.getMessage());
        }
    }
}
 Output:

Caught Exception: Negative संख्या स्वीकार्य नहीं है!
Custom Exception हमें खुद के Exception Define करने की सुविधा देता है।

NotesMedia App Icon

Get the NotesMedia App

Hindi & English Free Notes, Old Papers & AI Chats

Install