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.
Exceptions class extend
Let’s look at some of the similarities between both
- Both classes extends
java.lang.Throwableand thus, inherits many of the methods which are common to be used when dealing with errors such as:
printStackTraceand so on.
java.lang.Exceptioncan be declared in the method header, can be in
catchclause can be used with keyword
java.lang.Exceptionand its subclasses method header declaration is required using
throwskeyword for others like
java.lang.RuntimeExceptionand their subclasses the declaration is optional.
java.lang.Errordesigned 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,
OutOfMemoryErrorthis 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.Exceptionthe first is considered to be an unchecked exception for compile-time exception checking. As a result code throwing
java.lang.Erroror its subclasses don't require to declare this error in the method header. While throwing
java.lang.Exceptionthe 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.
Checked vs Unchecked Exception in Java Example
is two types of Exception exist in Java. Though there is no difference in functionality and you can very achieve same…