Sitemap

Member-only story

Difference Between Error and Exception in Java

3 min readApr 5, 2025

🔒 This is a Medium member-only article. You can read it for free on my blog: Difference between Error and Exception in Java.

In Java, when something goes wrong during the execution of a program, the Java Virtual Machine (JVM) throws an object that describes the problem. These problems are broadly categorized into two types:

  • Errors
  • Exceptions

Although both represent issues that occur during runtime, they are not the same, and it’s important to understand the differences between Errors and Exceptions for better debugging, exception handling, and application reliability.

In this article, we’ll clearly explain the difference between Errors and Exceptions in Java, with examples and a detailed comparison table.

✅ What is an Error in Java?

An Error in Java represents serious problems that a program should not try to handle. These errors are generally thrown by the JVM and indicate issues that are beyond the control of the program, such as memory leaks or stack overflow.

🔹 Technical Definition:

  • Errors are instances of the class java.lang.Error.
  • They are unchecked, meaning they are not checked at compile-time.
  • These typically occur in the runtime environment (e.g., JVM or hardware failure).

Examples of Errors:

  • StackOverflowError: Occurs when a method calls itself repeatedly without a proper stopping condition.
  • OutOfMemoryError: Occurs when JVM cannot allocate memory for objects because heap space is exhausted.
public class ErrorDemo {
public static void main(String[] args) {
main(args); // causes StackOverflowError
}
}

✅ What is an Exception in Java?

An Exception represents conditions that a program might want to catch and handle. These are problems that can be anticipated and recovered from during execution using proper exception handling techniques like try-catch.

🔹 Technical Definition:

  • Exceptions are instances of the class java.lang.Exception.
  • They include both checked and unchecked exceptions.

--

--

No responses yet