Trusted answers to developer questions

What is the difference between errors and exceptions in Java?

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Errors and exceptions are subclasses of the Throwable Java class. The Error class represents critical conditions that can not be caught and handled by the code of the program. On the other hand, the Exception class represents concerning conditions raised by the application itself; these can be caught and handled within the code to ensure that ​the application continues to run smoothly.

svg viewer

Differences

The key differences between exceptions and errors are as follows:

Errors Exceptions
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. The use of try-catch blocks can handle exceptions and recover the application from them.
Errors occur at run-time and are not known by the compiler; hence, they are classified as “unchecked.” Exceptions can be “checked” or “unchecked,” meaning they may or may not be caught by the compiler.
“OutOfMemory” and “StackOverflow” are examples of errors. “IndexOutOfBounds” is an example of an unchecked exception, while “ClassNotFound” is an example of a checked exception.

RELATED TAGS

errors
exceptions
java
differences
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?