Exception in Java — Oversimplified

Saurav Samantray
2 min readJun 23, 2021

--

What is an Exception?

The meaning of Exception in Java or any other programming language is not different from its literal English meaning. A disruption or abnormality, which hinders the normal execution of a program.

Why an Exception?

Instead of diving into the hierarchy of Exceptions in a structured manner that probably thousands of other articles have already done and in a good way, let’s think like someone new to programming. Why would an exception or disruption occur?

Unchecked/Runtime Exception — What if the program hits a snag because it cannot compute any further? Say for example you force the code to divide by zero which mathematically cannot yield a valid result or try to access a non-existent index in an array? All such exceptions are termed as Unchecked or runtime exceptions. The compiler doesn’t force you to handle these exceptions and if not handled will cause your program to crash.

Checked/Compiletime Exception — Obviously, all the exceptions occur when the program is running, then why are some called Checked or more importantly Compile Time exceptions? Do these exceptions occur at compile time? No, even checked exception occurs at runtime, but checked exceptions are the ones that the compiler forces you to handle in order for you to be able to successfully compile the code. Take the example of FileNotFoundException. Whether a file is present not or not will be determined at runtime. However, as this is a checked exception, the compiler forces you to handle the exception in a catch block and provide instruction as to what you want the program to do if such an event occurs. It's easy to abuse these kinds of exceptions as the developer could beforehand catch and provide a workaround and not actually address the concern.

Errors — Most serious form of disruption which cannot be handled by your code and something that happens at the JVM level is termed as an error. If JVM runs OutOfMemory, StackOverflow, etc.

As with every oversimplified article, this series is meant to give you a peek into important concepts in the simplest of way esuring you start learning a concept with your basics right. It is important that you to do further reading to get a good grasp of these concepts in more details.

--

--

No responses yet