Home Java Tutorial Difference Between Error and Exception in Java

Difference Between Error and Exception in Java

by Anup Maurya
23 minutes read

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 COMPARISONERROREXCEPTION
BasicAn error is caused due to lack of system resources.An exception is caused because of the code.
RecoveryAn error is irrecoverable.An exception is recoverable.
KeywordsThere is no means to handle an error by the program code.Exceptions are handled using three keywords “try”, “catch”, and “throw”.
ConsequencesAs 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.
TypesErrors are classified as unchecked type.Exceptions are classified as checked or unchecked type.
PackageIn Java, errors are defined “java.lang.Error” package.In Java, an exceptions are defined in”java.lang.Exception”.
ExampleOutOfMemory, 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

  1. Error occur only when system resources are deficient whereas, an exception is caused if a code has some problem.
  2. An error can never be recovered whereas, an exception can be recovered by preparing the code to handle the exception.
  3. 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.
  4. 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.
  5. Errors are of unchecked type i.e. error are not in the knowledge of compilers whereas, an exception is classified as checked and unchecked.
  6. Errors are defined in java.lang.Error package whereas, an exception is defined java.lang.Exception.

related posts

Leave a Comment