जावा में एक्सेप्शन हैंडलिंग क्या होती है: Exception Handling in JAVA in Hindi

Exception Handling in JAVA in Hindi: जावा में “Exception handling एक बहुत ही पॉवरफुल मेकेनिसम है जिसका इस्तेमाल runtime errors को handle करने के लिए किया जाता है. सीधे शब्दों में कहें तो यह JAVA programing language में एक बहुत ही खास feature है जो कि program को run करते समय में वाले Exception या errors को handle कर सकता है”

जावा में एक्सेप्शन क्या होता है और एक्सेप्शन हैंडलिंग किसे कहते हैं (What is Exception Handling in Hindi)

Exception अर्थ एक abnormal condition होता है. जब जावा में किसी प्रोग्राम को run किया जाता है तो exception एक ऐसा event होता है जो कि उस program के normal flow को डिस्टर्ब करता है. यह एक ऐसा object है जो कि प्रोग्राम के run time पर आता है.

What is Exception Handling in Hindi

यह जावा में ClassNotFoundException, IOException, SQLException, RemoteException, जैसी run time error को हैंडल करने के लिए एक mechanism होता है.

Exception Handling के लाभ

किसी भी program या application में normal flow को बनाये रखना ही Exception Handling के प्रमुख लाभ में से एक है. Exception किसी भी program या application के normal flow को डिस्टर्ब करता है इसलिए हमने प्रोग्राम का flow बनाये रखने के लिए इसे Handle करने की जरूरत होती है. जिसके लिए Exception Handling mechanism  का इस्तेमाल किया जाता है.

Example

उदाहरण के लिए मान लो कि अगर किसी भी java program में 5 statement है और statement 3 में कोई Exception आ जाता है तो ऐसे में 4 और 5 statement execute ही नहीं होंगे. लेकिन जब हम exception handling करते हैं तो बाकि statements भी execute हो जाते हैं. इसलिए java में हम exception handling का इस्तेमाल करते हैं.

  1. statement 1;  
  2. statement 2;  
  3. statement 3; ;//exception occurs  
  4. statement 4;  
  5. statement 5

Java Exceptions Hierarchy

java में Java.lang.Throwable Class जावा एक्सेप्शन की मूल क्लास है जिसमे 2 sub class inherit होती हैं.

Types of JAVA Exception

java programing language में मुख्य रूप से दो प्रकार के Exception होते हैं checked और unchecked.

JAVA प्रोग्राम में आने वाली error को unchecked Exception कहा जाता है.हालाँकि, Oracle के अनुसार Exception 3 तरह के होते हैं.

  • Checked Exception
  • Unchecked Exception
  • Error

Checked और Unchecked Exceptions क्या होते हैं

checked Exceptions

RuntimeException और Error को छोड़कर जो भी clases, Throwable class को inherit करते हैं उन्हें Checked Exception कहा जाता है. checked Exceptions को program के compile-time पर चेक किया जाता है.

Example

IOException
SQLException

2) Unchecked Exceptions

Unchecked Exceptions ऐसी classes होती है जो RuntimeException को इनहेरिट करती हैं. आपको बात दें कि Unchecked Exceptions को program के compile time पर नहीं बल्कि run time पर चेक किया जाता है.

Example

ArithmeticException
NullPointerException
ArrayIndexOutOfBoundsException

Error

आपको बता दें कि error अपूरणीय (irrecoverable) होती है.

Example

 OutOfMemoryError
 VirtualMachineError
 AssertionError

Class Hierarchy

Java Exception Keywords in Hindi (Types of Exception Handling in Java in Hindi)

Java Programming में Exception को हैंडल करने के लिए 5 keyword प्रदान प्रदान किये जाते हैं जिनके बारे में हमने नीचे जानकारी दी है

Try
Catch
Finally
Throw
Throws

Try

try जावा में exception handling करने के लिए एक keyword है जिसका उपयोग उस block को निर्दिष्ट करने के लिए किया जाता है जिसमें हमे exception code को रखना है. हम try को अकेले उपयोग नहीं कर सकते हैं इसके साथ या ओ catch या finally का उपयोग किया जाना चाहिए.

catch

“catch” block का उपयोग catch exception को handle करने के इए किया जा सकता है. “catch” keyword का उपयोग हमेशा Try के बाद किया जाता है. इसका मतलब यह है कि हम “catch” block को अकेले उपयोग नहीं कर सकते हैं. “catch” block के बाद finally का उपयोग किया जा सकता है.

Finally

 “finally” block का इस्तेमाल प्रोग्राम के आवश्यक code को execute करने के लिए किया जाता है. उदाहरण के लिए जैसे file resources को close करना या फिर database connection को close करना. “finally” block का इस्तेमाल हमेशा Try और catch block के साथ किया जाता है. इसे आप Try के साथ अकेले भी उपयोग कर सकते हैं जैसे try-finally और try-catch के साथ भी उपयोग कर सकते हैं जैसे try-catch-finally.

आपको बता दें कि finally का इस्तेमाल catch के बाद किया जाता है. “finally” block को execute किया जाता है भले ही exception को handle हुआ हो या नहीं हुआ हो.

Throw 

exception को throw करने के लिए इस keyword का इस्तेमाल किया जाता है. आपको बता दें कि यह checked और unchecked दोनों एक्सेप्शन में use हो सकता है. मुख्य रूप से user-defined exception को throw करने के लिए यह keyword उपयोग किया जाता है.

Throws 

JAVA में exception को declare करने के लिए इस keyword का इस्तेमाल किया जाता है. code में try और catch block की जगह आप Exception को handle करने के लिए throws keyword का उपयोग कर सकते हैं. आपको बता दें कि throws keyword का उपयोग मुख्य रूप से checked exception को handle करने के लिए होता है. इसका इस्तेमाल method signatures के साथ किया जाना चाहिए.

Leave a Comment