Java Exception Handling In hindi

📔 : Java 🔗

Exception Handling , program में आयी unwanted runtime error को handle करने का एक process / method है। इन unwanted errors की वजह से कभी कभी program terminate हो जाता है। हालाँकि program का terminate होना Exception पर depend करता है कि Exception किस तरह की है।

Understanding Exception : Exceptions कुछ unwanted events होती हैं , जो program execution के समय आती हैं और program को disrupts/terminate करती हैं।

Advantage of Exception Handling

किसी भी programming language में exception handling का सबसे बड़ा advantage है application के normal flow को maintain करना। किसी भी type की error आते ही program का execution stop हो जाता है जिसकी वजह से हमें desired output नहीं मिलता है।


suppose कीजिये आपके program में multiple statements हैं तो अगर कही बीच में error आयी तो उससे आगे का code run नहीं होगा।

statement 1;  
statement 2;  
statement 3; // any runtime error 
statement 4;  
statement 5;
statement 6;

Error vs Exception

technically देखा जाये तो Error और Exception दोनों different हैं। Errors , code syntax या system resources की कमी की वजह से आती हैं , इन errors को आप handle नहीं कर सकते हैं। जबकि Exception आपके logic या condition की वजह से आती हैं , ये runtime errors हैं जिन्हे Run Time Exception कहते हैं , exceptions को आप handle कर सकते हैं।

Java types of error

  1. Syntax Errors
  2. Run Time Errors (Exceptions)
  3. Logical Errors

Java Syntax Error

Java में Syntax Errors program में syntax सही न होने के कारण आती हैं , इन्हे compile time error भी कहते हैं जो कि compile time पर आती हैं।
For Example -

// forgot to use semicolon at the end of statement.
System.out.println("Hello, World!")

HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")
^
1 error

ये errors program में होने पर हम , program को run करना तो दूर compile भी नहीं कर सकते हैं।

Java Logical Error

Logical Errors , program में apply किये गए logic में mistake की वजह से आती हैं। इन Logical Errors की वजह से हमें desirable output नहीं मिलता। Logical Errors को बिना किसी tool के handle करना मुश्किल है।
For Example -

// infinite loop
for(int i=1; true; i++);{
  // code.
}

ऊपर दिए गए example में हमने infinite for loop run किया है , तो logically ये loop कभी end ही नहीं होगा , जिससे हमें desired output न मिले या शायद कोई output न मिले।

Java Runtime errors

Runtime Errors , program के running time पर आती हैं , run time errors को ही exceptions कहते हैं। और सिर्फ इन्ही errors को हम handle कर सकते हैं।

Java Exception classes Hierarchy

Java Exception hierarchy के लिए java.lang.Throwable class root class होती है जिसे दो sub classes inherit करती है Exception और Error , और इस तरह से अलग - अलग error के according अलग - अलग sub classes ने इन class को inherit किया , जैसा कि नीचे दिखाया गया है।

            Throwable
                |
                |
    -------------------------------------
    |                                   |
    |                                   |
Exception                             Error
    |                                   |                      
    └── IOException                     └── StackOverflowException
    |                                   |
    └── SQLException                    └── VirtualMachineException
    |                                   | 
    └── ClassNotFoundException          └── OutOfMemoryException 
    |
    └── RuntimeException
            |
            └── ArithmeticException
            |
            └── NullPointerException
            |
            └── NumberFormatException
            |
            └── IndexOutOfBoundException
                    |
                    └── ArrayOutOfBoundException
                    |
                    └── StringOutOfBoundException

Java Types of Exceptions

java में mainly दो तरह की exception है - checked और unchecked. Error को as an unchecked exception consider किया जाता है। हालाँकि Oracle के according exception 3 तरह की होती है।

  • Checked Exception
  • Unchecked Exception
  • Error

Java Checked Exception

RuntimeException और Error को छोड़कर जो classes directly Throwable class को inherit करती हैं उन्हें checked exceptions कहते हैं , जैसे : IOException, SQLException etc. checked exceptions compile time पर check की जाती है।

Java Unchecked Exception

वो classes जो RuntimeException को inherit करती हैं उन्हें Unchecked Exception कहते हैं , ये exception compile time पर check न होकर run time पर check होती है।

Java Error

जैसा कि आपने ऊपर भी पढ़ा कि Error irrecoverable होती हैं , mean इन्हे आप handle नहीं कर सकते हैं ये errors code syntax या system resources की कमी की वजह से आती हैं।

Java keywords for exception handling

Java exception handle करने के लिए कुछ predefined keywords का use किया जाता है , जो कि इस प्रकार हैं -

KeywordDescription
try

try block के अंदर हम अपना सारा code / logic लिखते हैं जिसमे exception आने के chances हैं , मतलब हम जो implementation code लिखेंगे वो try block में ही होना चाहिए। try को हमेशा catch या finally के साथ use करते हैं।

catch

catch का use exception को handle करने के लिए करते हैं , इसे हमेशा try block के साथ ही use करते हैं। single try block के साथ आप error type के according आप एक से ज्यादा catch भी use कर सकते हैं।

finally

finally block में वो code लिखते है जिसे हमेशा run करना हो , चाहे error आये या नहीं। इसे भी हमेशा try block के साथ ही use करते हैं।

throw

throw keyword का use custom exception को throw / create करने के लिए किया जाता है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers