Table of Contents
In this article, we’ll discuss about Difference Between Error and Exception in Java.
The major difference between error and exception is that an error is caused due to lack of system resources, and an exception is caused because of your code. Let us study other differences between error and exception along with a comparison chart.
Difference Between Error and Exception in Java
BASIS FOR COMPARISON | ERROR | EXCEPTION |
---|---|---|
Basic | An error is caused due to lack of system resources. | An exception is caused because of the code. |
Recovery | An error is irrecoverable. | An exception is recoverable. |
Keywords | There is no means to handle an error by the program code. | Exceptions are handled using three keywords “try”, “catch”, and “throw”. |
Consequences | As the error is detected the program will terminated abnormally. | As an exception is detected, it is thrown and caught by the “throw” and “catch” keywords correspondingly. |
Types | Errors are classified as unchecked type. | Exceptions are classified as checked or unchecked type. |
Package | In Java, errors are defined “java.lang.Error” package. | In Java, an exceptions are defined in”java.lang.Exception”. |
Example | OutOfMemory, StackOverFlow. | Checked Exceptions : NoSuchMethod, ClassNotFound. Unchecked Exceptions : NullPointer, IndexOutOfBounds. |
Exception through a Java program
Let’s understand the exception through a Java program.
import java.util.Scanner;
public class ExcptionExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.println("You have entered: "+number);
}
}
Let’s run the above program and enter a float value deliberately to generate an exception.
Enter a number: 12.5 Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at ExcptionExample.main(ExcptionExample.java:8)
It shows the InputMismatchExaception. Because the program accepts an integer value. We observe that the next statement is skipped and the program is terminated.
Error through a Java program
Let’s understand the Error through a Java program.
public class ErrorExample
{
public static void main(String args[])
{
//method calling
recursiveDemo(10);
}
public static void recursiveDemo(int i)
{
while(i!=0)
{
//increments the variable i by 1
i=i+1;
//recursive called method
recursiveDemo(i);
}
}
}
Let’s run the above program to see an error.
Exception in thread "main" java.lang.StackOverflowErrorat ErrorExample.recursiveDemo(ErrorExample.java:15) at ErrorExample.recursiveDemo(ErrorExample.java:15) at ErrorExample.recursiveDemo(ErrorExample.java:15) at ErrorExample.recursiveDemo(ErrorExample.java:15)
We observe that on running the program, we get the StackOverflowError, not an exception.
Key Differences Between Error and Exception
- Error occur only when system resources are deficient whereas, an exception is caused if a code has some problem.
- An error can never be recovered whereas, an exception can be recovered by preparing the code to handle the exception.
- An error can never be handled but, an exception can be handled by the code if the code throwing an exception is written inside a try and catch block.
- If an error has occurred, the program will be terminated abnormally. On the other hand, If the exception occurs the program will throw an exception, and it is handled using the try and catch block.
- Errors are of unchecked type i.e. error are not in the knowledge of compilers whereas, an exception is classified as checked and unchecked.
- Errors are defined in java.lang.Error package whereas, an exception is defined java.lang.Exception.