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.
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
Exception | Error | |
---|---|---|
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.