6 Difference between Error and Exception in Java with Examples

Difference between Error and Exception in java is one of the most common interview questions for java beginners. Errors are the conditions that can not be handled and irrecoverable. Every time when Error occurs, the program terminates abruptly. Errors belong to the Unchecked category. It occurs at runtime only. On the other hand, Exceptions are the conditions that can be handled using try/catch, throw keywords. Exceptions can occur at compile-time or runtime. It is divided into two categories Unchecked Exception and Checked Exception. In this tutorial, I will be sharing Error vs Exception, an example of Error, an example of Exception and similarities between them.

Read Also : throw vs throws in java

Difference between Error and Exception in Java

1.Time of occurrence : Exceptions can occur at compile time or runtime, depending on the type of exception occurred. For example, NullPointerException is a runtime exception on the other hand IOException is a compile-time exception.

Errors occur only at runtime. They are not known to the compiler.

Read Also : Programs on Exception Handling in Java

2. Recovery : Programs can recover from Exceptions by handling them appropriately using a try-catch block or throw keyword in java.

Programs can not recover from Errors once they occur. Errors will definitely cause termination of the program.

3. Package : Exceptions are defined in java.lang.Exception package.

Errors are defined in java.lang.Error package.

Exception class hierarchy



















4. Checked/Unchecked : Exceptions can be Checked(compile-time exceptions) or Unchecked exceptions(runtime exceptions). You can find the difference between Checked and Unchecked Exception here.

Errors are a part of Unchecked exceptions in java.

5. Cause : Exceptions are caused by the program/application itself.

Errors are caused by the environment in which the program runs.

6. Examples : Checked Exception examples are IOException, SQLException, etc
Unchecked Exception examples are NullPointerException, ArrayIndexOutOfBoundException etc.

Errors examples are java.lang.OutOfMemoryError, java.lang.StackOverflowError etc.

Example of Error and Exception in Java

Example of Error :

The below example throws java.lang.StackOverflowError.

public class JavaHungry {
    public static void main(String args[])
    {
        /*Below line calls test method
          which produces StackOverflow error*/
        ThrowError.test(100);
    }
}

class ThrowError {
    public static void test(int num)
    {
        /* Recursion never reach the base 
           condition, generate StackOverflow
           error */
        if(num == 0)
            return ;
        else
            test(num++);
    }
}

Output :
Exception in thread "main" java.lang.StackOverflowError
    at ThrowError.test(JavaHungry.java:19)



Example of Unchecked Exception : 

The below example throws ClassCastException which is an Unchecked Exception.

public class UncheckedExceptionExample
{ 
     public static void main(String[] args){ 
      try
      {   
        int num = 20; 
        String str; 
        Object obj; 
        obj = new Integer(num); 
        System.out.println("integer value : "+ obj); 
        str = (String)obj;   //This will cause ClassCastException 
        System.out.println("string value : "+ str);   
      }   
      catch(Exception e)
      { 
        e.printStackTrace(); 
      } 
     } 
} 

Output :
integer value : 20

java.lang.ClassCastException: java.base/java.lang.Integer cannot be cast to java.base/java.lang.String
    at UncheckedExceptionExample.main(UncheckedExceptionExample.java:11)


Similarities Between Error and Exception in Java

1. Both Error and Exception are subclasses of the Throwable class.

2. Both Error and Exception can occur at runtime.

Recap : Difference between Error and Exception in Java



ExceptionError
Time of occurrenceOccur at compile time or runtimeOccur at runtime.
RecoveryPrograms are recoverable from Exceptions by handling them appropriately using try/catch block or throw keyword in java.Programs are irrecoverable from Errors once they occur.
PackageExceptions are defined in java.lang.ExceptionErrors are defined in java.lang.Error
Checked/UncheckedExceptions can be both Checked as well as Unchecked exceptions.Errors belong to the Unchecked type.
CauseCaused by application/programCaused by the environment in which the program runs.
ExamplesChecked Exception examples are IOException, SQLException and Unchecked Exception examples are NullPointerException, ArrayIndexOutOfBoundExceptionjava.lang.OutOfMemoryError, java.lang.StackOverflowError

That's all for today, please mention in comments if you have any questions or doubts regarding the difference between error and exception in java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry