What is Exception Handling and types of Errors in JAVA.


Exception Handling:

Exception handling means handling the errors that gets occurs during coding a program. As, we are humans, we commit errors while making a program. So, we need to handle the errors. In, programming language, errors are also called as
"BUGS". The process of removing them is called as "DEBUGGING".


Errors in JAVA:

1). Compile-time error: The errors which comes when we compile a program are known as compile time error. These may arise due to wrong syntax.

Example: In the below example, we have not put a semicolon at the end of printing statement.
public class Test{

    public static void main(String[] args)  {

    //if we print a statement without ; it will throw an error

        System.out.println("Hello World")

    }

}
Output:
Error: java: ';' expected
So, it will through the error and it is detected by JAVA compiler.
2). Run time Error: These errors occur after compiling a program. These may occur due to insufficient memory to store something or inability of the microprocessor to execute some statements come under run-time errors. This program will get fully compiled, but at run time it will through an error
.
Example:
public class Test{

    public static void main(String args[])  {

    //this will give logical error

      int a = 10;

      int b = 0;

      int c = a/b;

        System.out.println(c);

    }

}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Test.main(Test.java:6)
So, this will generate error, because we know that any number can't be divided by 0.


3). Logical Error: Logical errors occur due to error in logic. In fact, these are not detected at Compile-time or Run Time.The programmer needs to pay attention while writing a logic part in a program.
If a programmer writes a wrong logic, the output of the program will give the wrong answer.

Example:

public class Test{
public static void main(String args[]) {
//this will give logical error

      
int side=2;

int area = side*side*side; //logical error

System.out.println(area);
}
}
Output:
8

Process finished with exit code 0

Explanation: As, we know that area of square is calculated by formula, side*side. Rather, here we have done side*side*side which give rise to logic error in program. So, program must keep attention while writing logic part to avoid logic error in a program. 

Also read:


Previous Post Next Post

Contact Form