Difference Between Errors and Exceptions in Java

Difference Between Errors and Exceptions in Java

3 mins readComment
Anshuman
Anshuman Singh
Senior Executive - Content
Updated on Apr 18, 2025 08:23 IST

In Java, one of the main difference between errors and exceptions is that errors represent severe issues that indicate resource exhaustion or system problems beyond the program's control. Recovering from an error is generally impossible. On the other hand, exceptions represent unexpected events that occur during program execution due to issues within the code itself. These can often be handled using try-catch blocks to provide alternative behaviour or informative messages.

difference between errors and exceptions

In Java, both errors and exceptions are subclasses of the Java Throwable class that belongs to java.lang package. Even though both are the subclass of the same class, they represent distinct issues that can arise during program execution. 

Table of Content (TOC)

Difference Between Errors and Exceptions

For better clarity, let's comprehend the difference between these two subclasses in a tabular format.

Aspect Error Exception
Definition A serious problem that cannot be recovered from, typically arising from system-level issues. An issue that can disrupt the normal flow of a program but can be caught and handled.
Origin System abnormalities such as hardware failures, system crashes, or out of memory. Application code, including invalid input or incorrect API usage.
Recovery Fatal and non-recoverable. Can often be recovered from using try-catch blocks.
Types Syntax Error, Runtime Error, Logical Error. Checked Exceptions (detected at compile time) and Unchecked Exceptions (occur at runtime).
Examples OutOfMemoryError, StackOverflowError. IOException, NullPointerException, SQLException, etc.
Handling Cannot be handled or caught by the program. Can be caught and handled in the program to maintain flow or recover from the situation.
Hierarchy in Java Extends the Error class in in Java’s class hierarchy. Extends the Exception class in the hierarchy.
When They Occur Can occur both at compile time and runtime. Primarily occur at runtime, though checked exceptions can be detected at compile time.
Impact May cause the program (and potentially the system) to terminate. Disrupts the normal flow but allows for redirection or handling within the program.
Predictability Unpredictable and often outside the control of the application. Can be anticipated and handled through proper coding practices.

You should also explore: Features of Java Programming Language

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

Free
6 months
– / –
350 hours
Free
2 hours
Free
2 hours
Free
9 hours
– / –
6 weeks
Free
55 hours
Free
25 hours
– / –
– / –
– / –
4 months

What are Errors?

Errors in Java denote serious issues that majorly occur at runtime due to external factors beyond the program's control. Common causes of these errors include critical system failures and resource limitations, such as running out of memory or encountering a stack overflow. 

Due to their gravity, errors are represented by the Error class and its subclasses. Attempting to recover from them within the program is nearly impossible. Instead, the recommended approach is to log the error and exit the application. Some common examples of Java errors are:

  • OutOfMemoryError: Occurs when the Java Virtual Machine (JVM) exhausts available memory resources.
  • StackOverflowError: Arises when the call stack becomes too deep due to excessive method invocations.
  • NoClassDefFoundError: Triggered when the JVM cannot locate a required class.

Error Example: OutOfMemoryError

public class ErrorExample {
public static void main(String[] args) {
try {
// Intentionally create an array size that is too large to fit into memory
int[] largeArray = new int[Integer.MAX_VALUE];
} catch (Throwable e) {
// Catch the OutOfMemoryError
System.out.println(e.toString());
}
}
}
Copy code

Output:

Errors in Java Example Output

Explanation: This example tries to allocate an array that's too large for the JVM's memory, causing an OutOfMemoryError. This error is caught by catching Throwable (the superclass of all errors and exceptions), and its type is printed. Usually, catching such errors is not recommended unless you're doing specific error handling or logging, as it indicates issues that an application usually cannot recover from by itself.

What are Exceptions?

Exceptions in Java are issues that occur within a program and disrupt the normal flow of its execution. These can be recoverable errors like NullPointerException and IllegalArgumentException. Java has two types of exceptions: checked and unchecked. They differ in their handling requirements. 

  • Checked Exceptions: These are known to the compiler at compile time, necessitating explicit handling within the code.
  • Unchecked Exceptions: These are detected at runtime and are typically caused by programming errors, allowing for more flexibility in handling.

Developers can use try-catch blocks to manage exceptions effectively. This allows them to provide informative error messages to users and prevent program crashes.

Must Explore: Exception Handling in Java

Exception Example: FileNotFoundException

import java.io.File;
import java.io.FileReader;
public class ExceptionExample {
public static void main(String[] args) {
try {
// Attempt to open a file that does not exist
File file = new File
Copy code
About the Author
author-image
Anshuman Singh
Senior Executive - Content
Anshuman Singh is an accomplished content writer with over three years of experience specializing in cybersecurity, cloud computing, networking, and software testing. Known for his clear, concise, and informative wr Read Full Bio