Prashant Shubham

Prashant Shubham

Computers are all I want to know about

01 Jun 2021

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:

Differences:

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: