Page 393 - CITS - Computer Software Application -TT
P. 393

COMPUTER SOFTWARE APPLICATION - CITS




                       catch (UserDefinedException ude)
                       {
                           System.out.println(“Caught the exception”);
                           // Print the message from MyException object

                           System.out.println(ude.getMessage());
                       }
                   }
               }

           Output










            Difference between final, finally and finalize
            The final, finally, and finalize are keywords in Java that are used in exception handling. Each of these keywords
            has a different functionality. The basic difference between final, finally and finalize is that the final is an access
            modifier, finally is the block in Exception Handling and finalize is the method of object class.

            Along with this, there are many differences between final, finally and finalize. A list of differences between final,
            finally and finalize are given below:

              S.No.          Key                 Final                 Finally                Finalize
                                        final is the keyword and   finally is the block   finalize is the method
                                        access modifier which is  in Java Exception    in Java which is used
                                        used to apply restrictions  Handling to execute the  to perform clean up
                1          Definition
                                        on a class, method or   important code whether  processing just before
                                        variable.               the exception occurs or  object is garbage
                                                                not.                   collected.
                                        Final keyword is used   Finally block is always   finalize() method is used
                                        with the classes,       related to the try and   with the objects.
                2        Applicable to
                                        methods and variables.  catch block in exception
                                                                handling.
                                        (1) Once declared,      (1) finally block runs the  cla
                                        final variable becomes   important code even if
                                        constant and cannot be   exception occurs or not.
                                        modified.               (2) finally block cleans
                3        Functionality  (2) final method cannot   up all the  resources
                                        be overridden by sub    used in try block
                                        class.
                                        (3) final class cannot be
                                        inherited.
                                        Final method is executed  Finally block is     finalize method is
                                        only when we call it.   executed as soon as    executed just before the
                                                                the try-catch block is   object is destroyed.
                4         Execution                             executed.
                                                                It’s execution is not
                                                                dependent on the
                                                                exception.


                                                           380

                             CITS : IT&ITES - Computer Software Application - Lesson 101 - 108
   388   389   390   391   392   393   394   395   396   397   398