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

COMPUTER SOFTWARE APPLICATION - CITS




                     try {

                       System.out.println(“Inside try block”);
                     // below code throws divide by zero exception
                      int data=25/0;
                      System.out.println(data);

                     }
                     // handles the Arithmetic Exception / Divide by zero exception
                     catch (ArithmeticException e){
                       System.out.println(“Exception handled”);
                       System.out.println(e);

                     }
                     // executes regardless of exception occurred or not
                     finally {
                       System.out.println(“finally block is always executed”);
                     }

                     System.out.println(“rest of the code...”);
                     }
                   }
           Output:
















           Java finalize Example
           FinalizeExample.java

           public class FinalizeExample {
                      public static void main(String[] args)

                   {
                       FinalizeExample obj = new FinalizeExample();

                       // printing the hashcode

                       System.out.println(“Hashcode is: “ + obj.hashCode());
                       obj = null;

                       // calling the garbage collector using gc()






                                                           382

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