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

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



           अ ास 108: उपयोगकता  प रभािषत इ े शन को संभालने के  िलए “throw” और “finally” कीवड
                         का उपयोग कर  (Use the “throw” and “finally” keywords handle user defined

                         exceptions)

            उ े
           इस अ ास के  अंत म  आप यह कर सक  गे
           •   उपयोगकता  प रभािषत इ े शन को संभालने के  िलए “throw” और “finally” कीवड  का उपयोग करके  Java  ो ाम डेवलप कर ।


           आव कताएं  (Requirements)

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


            ि या (Procedure)

           // Custom exception class
           class CustomException extends Exception {
               public CustomException(String message) {
                   super(message);

               }
           }
           public class ThrowFinallyExample {
               public static void main(String[] args) {
                   try {
                       // Simulating a condition where a custom exception is thrown

                       throwCustomException();
                   } catch (CustomException e) {
                       System.out.println(“Caught CustomException: “ + e.getMessage());
                   } finally {
                       // Code in the finally block will always be executed

                       System.out.println(“Finally block: This will execute whether an exception is thrown or not.”);
                   }
               }


               // Method that throws a custom exception
               private static void throwCustomException() throws CustomException {

                   // Using the ‘throw’ keyword to throw a custom exception
                   throw new CustomException(“This is a custom exception.”);
               }
           }




                                                           158
   167   168   169   170   171   172   173   174   175   176   177