Page 123 - ComputerScience_Class_11
P. 123

// Method defined here
                    }
                 Java finally block
                 A finally keyword follows after the try block. This block, if present, always executes whether there is an exception or
                 not. It appears at the end of the catch block.
                 Syntax of finally block:
                    finally
                    {
                      //Statement that has to be executed even if there is no error
                    }

                  Program 4      Write a program to divide two numbers without exception handling.


                   1      class withoutexceptionhandling
                   2      {

                   3          public static void main(String[] args)
                   4          {

                   5              int a=44, b=0,result;
                   6              result=a/b;

                   7              b=2;
                   8              result=a/b;

                   9              System.out.println("Answer : "+result);
                  10          }
                  11      }


                 The output of the preceding program is as follows:

                         BlueJ: Terminal Window - Java
                     Options


                    java.lang.ArithmeticException: / by zero
                            at withoutexceptionhandling.main(withoutexceptionhandling.java:6)





                  Program 5      Write a program to divide two numbers with exception handling.

                   1      class exceptionhandling

                   2      {
                   3          public static void main(String[] args)
                   4          {

                   5              int a=44,b=0,result;
                   6              try




                                                                                                          Objects  121
   118   119   120   121   122   123   124   125   126   127   128