Exceptions vs Errors in Java
In this article, we will be having a detailed look at exceptions and errors in Java. Let’s start with the most basic question What are exceptions??
Exceptions are in layman’s terms are misbehavior in an application that can be predicted and thus, handled to stop the program from getting terminated abruptly. The next question must be so What are errors??
In general, an error is which nobody can control or guess when it occurs. Errors are abnormal conditions that should never occur.
The above diagram depicts how Exception and Error classes are devised in Java. Error
and Exceptions
class extend Throwable
class.
Let’s look at some of the similarities between both Error
and Exception
class.
Similarities:
- Both classes extends
java.lang.Throwable
and thus, inherits many of the methods which are common to be used when dealing with errors such as:getMessage
,getStackTrace
,printStackTrace
and so on. - Both
java.lang.Error
andjava.lang.Exception
can be declared in the method header, can be incatch
clause can be used with keywordthrow
. Forjava.lang.Exception
and its subclasses method header declaration is required usingthrows
keyword for others likejava.lang.Throwable
,java.lang.Error
andjava.lang.RuntimeException
and their subclasses the declaration is optional.
Differences:
- ==
java.lang.Error
== ==designed to be thrown by the JVM and it indicates serious problems and intended to stop the program execution instead of being caught, it’s possible to catch the errors however, we shouldn’t attempt to as it’s thrown by JVM and there is not much as a programmer we can do about it.== For e.g,OutOfMemoryError
this error occurs when JVM can’t allocate any more memory for the object and the garbage collector doesn’t can’t make more memory available. Catching this error won’t solve any problem rather lead to an error-prone application. java.lang.Error
andjava.lang.Exception
the first is considered to be an unchecked exception for compile-time exception checking. As a result code throwingjava.lang.Error
or its subclasses don’t require to declare this error in the method header. While throwingjava.lang.Exception
the required declaration in the method header.
Since we have discussed major differences and similarities between the two, let’s look at one more quirky question.
At many places, even in java doc, you’ll find statements saying “Runtime exceptions and Errors are unchecked exceptions” — the statement is totally true but the exception part here doesn’t correspond to the Exception class name and more to “exception” as a concept or anomaly in code. Error if correctly specified can be more said as “Unchecked Throwable”.
The difference between “Unchecked Exceptions” and “Unchecked Throwables” is that your code shouldn’t throw an error as for the reason we’ve already discussed why above.
Now the question comes **What is an Unchecked Exception?
**Unchecked Exceptions in Java are those exceptions whose handling is not verified during Compile time. Unchecked Exceptions mostly arise due to programming errors like accessing the method of a null object, accessing elements outside an array bounding, or invoking methods with illegal arguments. So, Why Runtime Exceptions are unchecked Exceptions?
Runtime exceptions can occur anywhere in a program, and in a typical one, they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program’s clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).
We will discuss in detail Exceptions and their types in upcoming parts. Hope this article was helpful for you. Do refer links below for more insights.
Reference Links:
- https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html#:~:text=Because%20the%20Java%20programming%20language,exception%20subclasses%20inherit%20from%20RuntimeException%20
- https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html#:~:text=Class%20Error&text=An%20Error%20is%20a%20subclass,not%20try%20to%20catch%20it