Error in java with example

Exceptions or errors are most commonly facing problems in java language or any technology. In real time software, while designing the system or developing the code also commits several errors. These error are called as 'bugs' and the process of removing them is called 'debugging'.

Errors in Java program:
       There are basically 3 types of errors in Java Program:
                      1. Compile-time errors
                      2. Runtime errors
                      3. Logical errors

Compile-time Errors:
       These are syntactical error found in the code, due to which a program fails to compile. For example, forgetting a semicolon declaration at the end of the statement in Java program, or writing a program without proper syntax which result in compile-time error.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.javatbrains.exceptions;

public class CompileError {

     public static void main(String[] args) {
        System.out.println("Hello")
         System.out.println("Where is error")
     }

}

            In the above program we are not declared semicolon(;) at the end of the statements. This will be detected by the Java compiler at the compilation time.

Runtime Error:
       These errors represents inefficiency of the computer system to execute a particular statement. For example, insufficient memory for memory to store something or inability of the microprocessor to execute some statement come under run-time errors. 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.javatbrains.exceptions;

public class RuntimeError {

     public static void main() {
          System.out.println("Hello");
          System.out.println("Where is error");
     }

}

            Runtime errors are not detected by Java Compiler. They are detected by the JVM, only at runtime.

Logical Errors:
       These errors depict flaws in the logic of the program. The programmer might be using a wrong formula or the design of the program itself is wrong. Logical errors are detected by Java Compiler or JVM. The programmer is solely responsible for them.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
package com.javatbrains.exceptions;

public class LogicalError {

     public static void main(String[] args) {
          double sal = 5000.00;
          sal = sal * 15/100; //wrong use: sal += sal* 15/100; 
          System.out.println("Total salary = "+sal);
     }

}
OutPut:
     Total salary = 750.0

            By comparing the output of a program with manually calculated results, a programmer can guess the presence of a logical error.


Do you know?

Comments

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example