Exceptions in Java

Bindu Shrestha
4 min readOct 2, 2021

The term exception is shorthand for the phrase “exceptional event.”

Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.

I got this definition from the java documentation. I’m not sure how clear you are with the definition so I’ll do a bit of explaining here:

Let’s say we want string array to store courses available for CS students.

String[] courses ={“Web Development", "Electronic Communication”, “Microprocessors”};

And somewhere else in the program we try to access element at index 5.

System.out.println(courses[5]);

Since our array only contains 3 elements what should happen? Java has a way of handling this ‘exceptional event’; in a perfect world we should have never tried to access index that’s not there. But since we did and there’s no element at index 5, java creates an object of type ArrayIndexOutOfBoundsException and hands it to runtime system, in the world of java it’s called throwing an exception.

The exception object contains information related to the event as seen below.

First line tells the type of exception followed by description. Second line tells where this exception occurred, at line 14 in Example.java.

As we can see from the example, normal flow of the program should be to print the array element at given index. But since we try to print element which is not present, its an error and it disrupts the normal flow of program. Java handles it by throwing an exception object and terminates execution at the line exception occurred.

And since now we know what happened in our program we can go ahead and fix that..

In java there’s hierarchy of classes that represents different kind of events that can disrupt normal program flow. Below we will discuss the types of exceptions.

Exception hierarchy in java:

The class at the top of the exception class hierarchy is the Throwable class, which is a direct subclass of the Object class. Throwable has two direct subclasses — Exception and Error.

Error: These represent errors that occurs outside our program. For example if jvm runs out of memory, it throws OutOfMemory error and terminates. These occur during run time and application cannot recover from these.

Unchecked Exception(run-time): All sub classes of RunTimeException are unchecked exceptions. ArrayIndexOutOfBoundsException is a type of run time exception. These error can be avoided by the programmers as can be seen in above example.

Checked Exceptions(compile-time): These represents the kind of errors that should be anticipated and handled in code. All subclasses of Exception besides RuntimeException are checked exceptions. These are called checked exceptions since compiler checks if these are handled in the code. Compiler forces to handles these exception and gives error if not handled. Let’s take an example of checked exception.

Example:

If you check the implementation of readLine() method in BufferedReader class you can see that it throws IOException. It’s compile time exception and needs to be handled in the try catch block.

In our example since we have handled IOException in the try catch block, program does not terminate where the exception occurs. Instead it will execute the catch block and continue executing following lines. As can be seen below:

--

--