Exception Handling in Java एक्सेप्शन हैंडलिंग
एक्सेप्शन हैंडलिंग एक प्रभावी तकनीक है जो जावा प्रोग्राम को रन करते समय उत्पन्न होने वाली रनटाइम एरर को नियंत्रित करती है।
✅ 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
IOException
, SQLException
)IOException
, SQLException
)ArithmeticException
, NullPointerException
)ArithmeticException
, NullPointerException
)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
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!");elseSystem.out.println("Eligible to vote");}public static void main(String args[]) {try {validateAge(15);} catch (MyException e) {System.out.println("Caught Exception: " + e.getMessage());}}}
उपरोक्त कोड में हमने एक कस्टम एक्सेप्शन बनाया है।
Comments
Post a Comment