Page 171 - CITS - CSA - TP (Volume 2) - Hindi
P. 171

कं  ूटर सॉ वेयर ए ीके शन - CITS



           अ ास 107: म ी  try – catch  ॉकों का उपयोग कर  (Use multiple try – catch blocks)

            उ े
           इस अ ास के  अंत म  आप यह कर सक  गे
           •   अनेक try-catch  ॉकों का उपयोग करके  Java  ो ाम डेवलप करना।


           आव कताएं  (Requirements)
           उपकरण/साम ी (Tools/Materials)
           •   िवंडोज OS वाला PC/लैपटॉप
           •   SDK सॉ वेयर
           •   टे  एिडटर (िवजुअल  ू िडयो/सबलाइम/नोटपैड)
            ि या (Procedure)

           // programs using  multiple try – catch blocks
           public class MultipleTryCatchExample {
               public static void main(String[] args) {
                   try {
                       // First try block
                       int[] numbers = {1, 2, 3};
                       System.out.println(“Element at index 5: “ + numbers[5]); // This will throw ArrayIndexOutOfBoundsException
                   } catch (ArrayIndexOutOfBoundsException e) {
                       System.out.println(“Caught ArrayIndexOutOfBoundsException: “ + e.getMessage());
                   }


                   try {
                       // Second try block
                       String str = null;
                       System.out.println(“Length of the string: “ + str.length()); // This will throw NullPointerException
                   } catch (NullPointerException e) {
                       System.out.println(“Caught NullPointerException: “ + e.getMessage());
                   }

                   try {
                       // Third try block
                       int result = 10 / 0; // This will throw ArithmeticException
                       System.out.println(“Result of division: “ + result);
                   } catch (ArithmeticException e) {
                       System.out.println(“Caught ArithmeticException: “ + e.getMessage());
                   }
               }
           }

           आउटपुट:















                                                           157
   166   167   168   169   170   171   172   173   174   175   176