he term ‘Exception’ is short for “exceptional event”. In Java, an Exception is essentially an event that occurs during the execution of a program and disrupts the program’s normal flow. Exceptions are unwanted and mostly unexpected, which generally arise at either run- or compile-time. All of these Exceptions need to be handled to ensure the program runs in its natural flow.
However, before you handle Exceptions, it would help to know what different kinds of Exceptions are present with Java. Through this article, let’s look at the various types of Exceptions in Java and the Exception Hierarchy that they follow.
Check out our free courses to get an edge over the competition.
Exceptions Hierarchy in Java
The hierarchy of Exceptions in the Java programming language begins with the Throwable class – which comes from the Object class and is its direct subclasswhileThe Exception class presents all This Throwable class further branches into two subclasses – Error and Exception. Here’s a flowchart to understand the Java Exception hierarchy better:
The Exception class presents all the Exceptions that you might need to handle while working with the Java programming language. Some commonly known and encountered examples of such Exceptions include NullPointerException, ClassNotFoundException, IllegalArgumentException, etc.
On the other hand, the Error class takes care of more severe problems in your Java program architecture and is not taken care of within the application code. Some examples of errors in Java are InternalError, AssertionError, OutOfMemoryError, etc.
Check out upGrad’s Advanced Certification in Cyber Security
Exceptions in Java are further divided into two categories:
- Checked Exceptions – These are also known as compile-time exceptions.
- Unchecked Exceptions – These are also known as runtime exceptions.
One important point to note at this juncture is that unchecked Exceptions are all subclasses of the RuntimeException class. We will talk more about checked and unchecked exceptions later on in this article. But before that, let’s look at essentially how Errors and Exceptions differ in their working so that there is no confusion.
Errors and Exceptions in Java – How Do They Differ?
The official documentation of the Java programming language refers to Errors as occurrences during your Java programming that – “indicate serious problems that a reasonable application should not try to catch.” The seriousness of Errors is clear from the way this statement is poised. Clearly, this refers to the set of problems that your program might face that is not possible for it to recover from without either refactoring the code or modifying the Java application architecture.
Check out upGrad’s Advanced Certification in Blockchain
Let’s look at a Java method that is going to throw an error:
public static void print(String S) {
print(S);
}
In the code mentioned above, the method print() acts as a recursive method that keeps calling itself repeatedly, over and over again, until it reaches the maximum allowed size for a thread stack in Java. At that point, it exits execution with a very common – StackOverflowError, which reads something like:
Exception in thread “main” java.lang.StackOverflowError
at StackOverflowErrorExample.print(StackOverflowErrorExample.java:3)
As the above example shows, the method throws an example, but this error cannot be handled in the code itself. So, the program simply quits execution because the damage is irrecoverable. As a solution, the code needs to be modified.
Contrary to Errors, Exceptions indicate conditions that can be caught by a reasonable application. Exceptions in Java include issues that might occur either at the compile-time or during run time. These Exceptions happen rather frequently in all applications – especially during the testing and debugging phase. As a result, Exceptions in Java can be handled within the program itself to ensure that the code runs its natural flow.
Now, let’s talk a bit more about Exception Hierarchy in Java by looking at what checked and unchecked Exceptions are.
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
Checked and Unchecked Exceptions in Java
As mentioned earlier, Exceptions in a Java program can happen either during the compile-time or during the run time. This is what gives us the two broad types of Exceptions present in Java. Here’s looking at these two exceptions in detail.
Compile-time Exceptions
Exceptions that happen at the compile time are known as compile-time exceptions. These are also called checked exceptions because of the fact that you need to explicitly check them in your Java program and handle them in the code itself. Classes like InterruptedException, IOException, and more are checked exceptions.
Let’s look at a method that can handle a checked exception:
public void writeAFile() {
try (BufferedWriter b_w = new BufferedWriter(new FileWriter(“myFile.txt”))) {
b_w.write(“Testing”);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
As you can see, there are two statements in the above code’s try block –
- Instantiating the object – BufferedWriter
- Using the object to write to the file
Both these statements can throw IOException. IOException, being a Checked Exception, needs to be handled either by the caller or the method. In the above example, you can see the exception being handled within the method itself.
Explore Our Software Development Free Courses
Runtime Exceptions
Contrary to compile-time exceptions that are thrown during compile time, runtime or unchecked exceptions can be thrown “at any time”, which essentially means at runtime. As a result of this, methods don’t need to explicitly use catch-and-throw blocks to handle these unchecked exceptions. Some of the classes that inherit unchecked runtime exceptions include – NullPointerException, ArithmeticException, etc.
Let’s look at a piece of Java code that throws a NullPointerException unchecked Exception and that is not handled in the piece of code unlike earlier. Here it is:
public void writeToFile() {
try (BufferedWriter b_w = null) {
b_w.write(“Test”);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
When you call the above method, the program will throw a NullPointerException since the BufferedWriter object is null. Here is what the exception would read like:
Exception in thread “main” java.lang.NullPointerException
at IOExceptionExample.writeToFile(IOExceptionExample.java:10)
at IOExceptionExample.main(IOExceptionExample.java:17)
Explore our Popular Software Engineering Courses
In-Demand Software Development Skills
In Conclusion
With that, we come to the end of this blog on understanding the Exception Hierarchy in Java. Please keep in mind that this is by no means comprehensive coverage of Exceptions in Java and how to go about handling them. For that, you should dig deeper and expose yourself to different possible Exceptions to see how they work. Knowing Exception Handling will set you up for a successful career as a full-stack developer since Exceptions form the basics of any application.
At upGrad, we have a specialised full stack development course – Executive PG Programme in Software Development – Specialisation in Full Stack Development – where we take you through all the nuances of software development by focussing majorly on Full Stack development. We suggest you check out the course and probably book a seat soon!
Read our Popular Articles related to Software Development
1. Which class is present at the top of the Exception Hierarchy in Java?
The Throwable class is at the top of the Exception Hierarchy in Java.
2. What are the different kinds of Exceptions present in Java?
Java has unchecked as well as checked exceptions. Checked Exceptions are also called compile-time exceptions, whereas Unchecked Exceptions are also known as runtime exceptions.
3. How are exceptions handled in Java?
The try-catch is the easiest way to handle Exceptions in Java. The main piece of code that you want to be run needs to be put in the try block, while one or more catch blocks will catch all the exceptions that the code throws. This method will detect any type of Java exceptions that get thrown.