Page 133 - CA_Blue( J )_Class9
P. 133

This program prompts the user to enter two numbers, but instead of adding the numbers (which the user might
                 expect), it subtracts them due to a logical error in the code. This types of mistakes cannot be handled by the
                 compiler.


                 6.7.3 Runtime Error
                 Errors that occur during the execution of a successfully compiled program are called runtime errors. These errors
                 can happen when the user enters incorrect or unexpected values. Runtime errors can be detected and fixed during
                 the debugging process.


                  Program 10     Write a program to demonstrate runtime error.


                   1      import java.util.*;
                   2      class runtime_error

                   3      {
                   4      public static void main()
                   5      {

                   6          Scanner sc= new Scanner(System.in);
                   7          double a,b,q;

                   8          System.out.print ("Enter 1st number ");
                   9          a=sc.nextDouble();

                  10          System.out.print ("Enter 2nd number ");
                  11          b=sc.nextDouble();

                  12          q=a/b;
                  13          System.out.println("quotient : "+q);

                  14          }
                  15      }

                 You will get the following output:

                    Enter 1st Number 8
                    Enter 2nd Number 0
                    quotient infinity // error occurs as it is divided by 0 which is a runtime error

                     6.8 TESTING AND DEBUGGING
                 Testing is the process of identifying errors in the program code. After writing the code, errors may exist that need
                 to be removed. Therefore, it is important to verify and validate the code to identify these errors.
                 Debugging is the process of fixing the errors found during testing. It focuses on correcting these issues to ensure
                 the code works as expected.


                     6.9 EXCEPTION HANDLING IN JAVA
                 An exception is an abnormal condition that occurs when a runtime error happens. Exception handling is the
                 process of managing such errors during runtime. By using exception handling, the normal flow of the program can
                 be maintained. The try-catch block is used to implement exception handling.


                                                                                                   Input in Java   131
   128   129   130   131   132   133   134   135   136   137   138