Exceptions vs Errors in Java

Photo by Ricardo Gomez Angel on Unsplash

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. and class extend class.

Let’s look at some of the similarities between both and class.

Similarities:

  • Both classes extends and thus, inherits many of the methods which are common to be used when dealing with errors such as: , , and so on.
  • Both and can be declared in the method header, can be in clause can be used with keyword . For and its subclasses method header declaration is required using keyword for others like , and and their subclasses the declaration is optional.

Differences:

  • 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, 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.
  • and the first is considered to be an unchecked exception for compile-time exception checking. As a result code throwing or its subclasses don't require to declare this error in the method header. While throwing 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.

Computers are all I want to know about.