Page 89 - iprime_V2.2_class8
P. 89

One of the most commonly occurring runtime errors is the 'division by zero' error. It occurs
                 when we divide a number by zero. For example,

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

                        {
                               int a = 100;
                               int b = 0;

                               int c = a / b;
                               System.out.println(c);
                        }

                 }
                 If we compile the preceding program, it compiles successfully. But, when we run the program,
                 it will show an error message as follows:









                                                             Runtime Error
                 Logical Errors

                 A logical error is a type of error due to which a program compiles and executes successfully but
                 gives an unexpected or incorrect result. It is very difficult to find this type of error in the program.
                 The compiler will not be able to find logical errors. We need to read our programs deeply to
                 find logical errors. Logical errors are also called semantic errors. For example, if a programmer
                 accidentally adds two numbers instead of dividing them, then they will not get the expected output.

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

                        {

                               int a = 100;
                               int b = 200;

                               int c = a + b;

                               System.out.println("The result of division is: "+c);
                        }

                 }

                 When we compile and execute the preceding program, it does not show any error message.
                 However, it gives an incorrect output because we have used the plus (+) sign instead of the
                 divide (/) sign.



                                                                  Conditional, Loop and Jump Statements in Java    87
   84   85   86   87   88   89   90   91   92   93   94