Exception Handling in Java एक्सेप्शन हैंडलिंग

Exception Handling is an effective mechanism to handle runtime errors that may occur while executing a Java program.

एक्सेप्शन हैंडलिंग एक प्रभावी तकनीक है जो जावा प्रोग्राम को रन करते समय उत्पन्न होने वाली रनटाइम एरर को नियंत्रित करती है।

It ensures that the program flow remains normal even after unexpected errors.
यह सुनिश्चित करता है कि अप्रत्याशित त्रुटियों के बावजूद प्रोग्राम का सामान्य प्रवाह बना रहे।

It also helps provide user-friendly messages instead of abrupt program crashes.
यह उपयोगकर्ता को अचानक क्रैश की बजाय फ्रेंडली मैसेज देता है।

✅ Why Exception Handling is Important? एक्सेप्शन हैंडलिंग क्यों ज़रूरी है?

  • Prevents abrupt termination of programs प्रोग्राम को अचानक बंद होने से रोकता है

  • Helps identify bugs at runtime रनटाइम पर बग की पहचान में मदद करता है

  • Allows use of custom exception messages कस्टम एक्सेप्शन मैसेज का उपयोग संभव बनाता है

  • Enables graceful error recovery त्रुटियों से सरल पुनर्प्राप्ति को संभव बनाता है


📌 Basic Syntax बेसिक सिंटैक्स

try {
// Code that may throw exception ऐसा कोड जो एक्सेप्शन उत्पन्न कर सकता है
} catch (ExceptionType e) {
// Exception handler एक्सेप्शन को हैंडल करने वाला कोड
} finally {
// Cleanup code (optional) फाइनल क्लीनअप कोड (वैकल्पिक)
}


⚙️ Java Exception Class Hierarchy जावा एक्सेप्शन क्लास हाइरार्की

  • All exceptions in Java are derived from Throwable class. जावा में सभी एक्सेप्शन Throwable क्लास से प्राप्त होते हैं।

Throwable
/ \
Exception Error
/ \ \
Checked Unchecked JVM/System Errors


Checked Exceptions: Must be handled during compilation (e.g. IOException, SQLException)
चेक्ड एक्सेप्शन: इन्हें कंपाइल करते समय संभालना ज़रूरी होता है (जैसे IOException, SQLException)

Unchecked Exceptions: Occur at runtime (e.g. ArithmeticException, NullPointerException)
अनचेक्ड एक्सेप्शन: ये रनटाइम पर उत्पन्न होते हैं (जैसे ArithmeticException, NullPointerException)

Errors: Cannot be handled programmatically (e.g. OutOfMemoryError, StackOverflowError)
एरर: इन्हें प्रोग्राम के द्वारा नियंत्रित नहीं किया जा सकता (जैसे OutOfMemoryError, StackOverflowError)


🎯 Keywords Used in Exception Handling एक्सेप्शन हैंडलिंग में प्रयुक्त कीवर्ड

Keyword Meaning (अर्थ)
try Code block that may throw exceptionएक्सेप्शन उत्पन्न होने वाला कोड
catch Handles the exceptionएक्सेप्शन को संभालता है
finally Executes always, used for cleanupहमेशा चलेगा, क्लीनअप के लिए
throw Throws an exceptionएक्सेप्शन फेंकने के लिए
throws Declares exception in method signatureमेथड में संभावित एक्सेप्शन बताने के लिए

💡 Example 1: ArrayIndexOutOfBoundsException

import java.util.*;
class ExcepArrOutDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int arr[] = new int[10];
int n;
System.out.println("Enter the length of array");
try {
n = s.nextInt();
for(int i = 0; i < n; i++)
arr[i] = s.nextInt();
} catch (ArrayIndexOutOfBoundsException aioobe) {
System.out.println("ARRAY IS OVERFLOW");
aioobe.printStackTrace();
}
}
}

यह प्रोग्राम बताएगा कि यदि हम ऐरे से बाहर वैल्यू डालते हैं तो क्या होगा।


💡 Example 2: ArithmeticException

import java.util.*;
class ExcepDivDemo {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int a, b, c;
System.out.println("Enter numerator");
a = s.nextInt();
System.out.println("Enter denominator");
b = s.nextInt();
try {
c = a / b;
System.out.println("Result = " + c);
} catch (ArithmeticException ae) {
System.out.println("Denominator must not be zero");
ae.printStackTrace();
}
}
}

यह उदाहरण ज़ीरो से भाग देने पर एरर हैंडलिंग को दर्शाता है।


💡 Example 3: NullPointerException

class NullPointerDemo {
public void NullP() {
System.out.println("exception");
}
public static void main(String[] args) {
try {
NullPointerDemo np = null;
if(np == null)
throw new NullPointerException();
np.NullP();
} catch (NullPointerException npe) {
System.out.println("Null reference found");
npe.printStackTrace();
}
}
}

यह कोड दिखाता है कि अगर हम null ऑब्जेक्ट पर कोई मेथड कॉल करते हैं तो क्या होगा।


🔧 Create Your Own Exception

class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class TestCustomException {
static void validateAge(int age) throws MyException {
if (age < 18)
throw new MyException("You are underage!");
else
System.out.println("Eligible to vote");
}
public static void main(String args[]) {
try {
validateAge(15);
} catch (MyException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}

उपरोक्त कोड में हमने एक कस्टम एक्सेप्शन बनाया है।


🔚 Conclusion (निष्कर्ष)

Exception Handling is essential for writing robust and user-friendly Java applications.
एक्सेप्शन हैंडलिंग मज़बूत और यूज़र-फ्रेंडली एप्लिकेशन बनाने के लिए ज़रूरी है।

It improves error management, makes debugging easier, and maintains control over program execution.
यह एरर मैनेजमेंट को बेहतर बनाता है, डिबगिंग आसान करता है और प्रोग्राम को नियंत्रित रखता है।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध