Page 99 - computer science (868) class 11
P. 99

10              catch(ArithmeticException e)
                  11              {

                  12                  System.out.println("You cannot divide anything by zero");
                  13              }

                  14              b=2;
                  15              System.out.println("After assigning 2 to b, we get");

                  16              result=a/b;
                  17              System.out.println("Answer: "+result);
                  18          }

                  19      }

                 The output of the preceding program is as follows:
                 You cannot divide anything by zero
                 After assigning 2 to b, we get
                 Answer: 22

                 4.5.3 Input/Output Exceptions
                 Input/Output (I/O) exceptions are Java IOExceptions that occur whenever an input or output operation is unsuccessful
                 or interpreted. For example, while trying to read data from a file that is not present, Java would throw an I/O exception
                 name “FileNotFound”.
                 Some of the other exceptions are as follows:
                 •  ArithmeticException: If there is any error in the arithmetic operation then this error is thrown.
                 •  ArrayIndexOutOfBoundsException: It is thrown to indicate that an array has been accessed with an illegal index.
                 •  ClassNotFoundException: When we try to access a class, which is not present then this error is thrown.
                 •  NoSuchMethodException: When we try to access a method, which is not found then this error is thrown.
                 •  NullPointerException: This exception is raised when referring to the members of a null object. Null represents nothing.
                 •  InputMismatchException: This exception is raised when we try to enter one type of data to another which is not there.

                                                                                      #Interdisciplinary
                  Some More Programs                                                  #Experiential Learning


                   Program 1     To input principal amount, rate of interest and time and then calculate the simple interest.


                   1      import java.util.*;
                   2      class simple_interest

                   3      {
                   4          public static void main()

                   5          {
                   6              Scanner sc = new Scanner(System.in);

                   7              double si,p,r,t;
                   8              System.out.print("Enter the principal amount: ");



                                                                                                                        97
                                                                                                             Objects    97
   94   95   96   97   98   99   100   101   102   103   104