Java try-catch In Hindi

📔 : Java 🔗

Java programming language exceptions handle करने के लिए try - catch का use करते हैं।


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

Without try catch

हम जानते हैं कि किसी number में 0 से divide नहीं कर सकते हैं , तो अगर हम ऐसा code बिना try catch के लिखते हैं , तो कुछ इस तरह से error आती है।

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test 
{  
   public static void main(String[] args) 
   {  
     int result = 50/0; //may throw exception   
     System.out.println("rest of the code"); 
   }     
}
Output
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Test.main(Test.java:4)

Example में आप देख सकते हैं कि number को 0 से divide करने पर ArithmeticException exception generate हुई, जो कि एक Unchecked Exception है क्योंकि ArithmeticException class RuntimeException class को inherit करती है। जिस line में यह error आयी उससे आगे का code execute नहीं हुआ है चाहे 1000 lines code क्यों ही न लिखा हो। इसलिए हमें Exception handling की जरूरत पड़ी।

Java try-catch Example

अब ऊपर दिए गए example को अगर हम try-catch की help से handle करने की कोशिश करते हैं।

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test {  
  public static void main(String[] args) {  
    // now use try catch block.
    try {
       int result = 10/0;
    }
    catch(ArithmeticException error) {
      System.out.println("Error occurred : "+ error);
    }
    System.out.println("rest of the code is runnning...");
  }     
}
Output
Error occurred : java.lang.ArithmeticException: / by zero
rest of the code

तो example में देखकर आप समझ गए होंगे कि कैसे किसी run time exception को handle करते हैं , हालाँकि आप चाहे तो अपना पूरा code try block में भी रख सकते हैं।

well , हम जानते हैं कि किसी number को 0 से divide करने पर ArithmeticException आती है इसलिए catch block में इस class को use किया लेकिन आपको हर बार नहीं पता होगा कि आपके code में कौन सी error आएगी , ऐसे case में आप simply Exception class का use कर सकते हैं। और कोई customer error message भी print करा सकते हैं।
For example :

try {
  // code
}
catch(Exception error) {
   // your custom message.
}

by default exception catch होने पर आपको error object मिलत है , आप चाहे तो error message get करने के लिए getMessage() method का use भी कर सकते हैं।
For example :

try {
  System.out.println( 30/0 );
} 
catch (ArithmeticException error) {
  System.out.println("Error : " + error.getMessage());
}

//Output : Error : / by zero

Java multiple catch blocks

different - different exceptions को handle करने के लिए आप error type के according different - different classes का use भी कर सकते हैं।

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test {
  public static void main(String[] args) {
    try {
      int[] numbers = {12,23,34};
      // we knew that array length is 3.
      System.out.println( numbers[4]/0 );
    } 
    catch (ArithmeticException error) {
      System.out.println("Error : " + error.getMessage());
    } 
    catch (ArrayIndexOutOfBoundsException error) {
      System.out.println("Error : " + error.getMessage());
    }
    catch (Exception error) {
      System.out.println("Error : " + error.getMessage());
    } 
  }
}
Output
Error : Index 4 out of bounds for length 3

Java catch multiple exceptions

अगर आप कई exceptions को एक साथ single catch block में handle करना चाहते हैं तो simple | से सभी class names को catch block में pass कर सकते हैं।

File : Test.java

CopyFullscreenClose FullscreenRun
public class Test {
  public static void main(String[] args) {
    try {
      int[] numbers = {12,23,34};
      // we knew that array length is 3.
      System.out.println( numbers[4]/0 );
    } 
    catch (ArithmeticException | ArrayIndexOutOfBoundsException error) {
      System.out.println("Error : " + error.getMessage());
    }
  }
}
Output
Error : Index 4 out of bounds for length 3

I Hope, अब आपको Java में try catch का use करके exception handling अच्छे से समझ आ गया होगा।

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