What is the difference between error and exception name some error class and exception class?

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.

What is the difference between error and exception name some error class and exception class?

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 occurrence Occur at compile time or runtime Occur at runtime.
Recovery Programs 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.
Package Exceptions are defined in java.lang.Exception Errors are defined in java.lang.Error
Checked/Unchecked Exceptions can be both Checked as well as Unchecked exceptions. Errors belong to the Unchecked type.
Cause Caused by application/program Caused by the environment in which the program runs.
Examples Checked Exception examples are IOException, SQLException and Unchecked Exception examples are NullPointerException, ArrayIndexOutOfBoundException java.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.

What is the difference between error and Exception explain with example?

Errors are usually raised by the environment in which the application is running. For example, an error will occur due to a lack of system resources. Exceptions are caused by the code of the application itself. It is not possible to recover from an error.

What is an error class?

public class Error extends Throwable. An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.

What is the error in Java?

What is an Error in Java. In Java, an error is a subclass of Throwable that tells that something serious problem is existing and a reasonable Java application should not try to catch that error.