Exception in Java

  • Post author:
  • Post category:JAVA
  • Post comments:0 Comments

An exception is an issue that comes up during program execution. If an Anomaly happens, the program’s usual flow is interrupted and the program terminates abnormally, which is not recommended, and such exceptions should be controlled.

Exception can happen for a variety of reasons. Following are some scenarios where there is an exception.

  • Programmer enter invalid date then exception occur
  • Loss of Network connection can occur exception
  • JVM can be out of memory causes exception

Let’s discuss with an example below:

There are some examples shows the exception in java code which made programmer unpleasant and interrupt in flow of working.

  • Java.Lang.Array Index Out Of Bounds Exception.
IndexOutOfBounds Exception

Above, Array length is 5 so the last index is 4 (length — 1) then iterates for the loop until the fifth index.

  • Java.Lang.Null Pointer Exception.
Null Pointer Exception

In above, Student array is initialized with the size five but not able to assign values for name and age attributes without initializing zeroth index student object creator.

  • Java.Lang.Arithmetic Exception:/by null or zero
Arithmetic Exception

At 56/a argument, the java interpreter will be confused as a = 0 and interpreter cannot get an exact answer for 56/a.

Types of Exception:

There are three type of exception which normally occur during programming.

Checked Exception.

Exceptions other than Run time Exceptions are called Checked Exceptions because they are tested by the compiler during compilation to see whether the programmer has treated them or not. If the system doesn’t handle such exceptions, you will get a compilation. Below are the examples of checked exceptions,

SQLException,  IOException, ClassNotFoundException

Unchecked Exception.

Such exceptions are not tested at compile-time and the compiler does not check nor programmer handled them, but it’s programmer ‘s duty to manage such exceptions and have a secure escape.OutofBoundException, NullpointerException and ArithematicException are example of unchecked Exception.


Error is not like Exception at all, but issues that arise beyond user or programmer control. Errors are typically ignored in your code as there is rarely anything you can do about an error. For instance, if there’s a stack overload, an error may occur. They ‘re also ignored when compiling.

Exception Inheritance Hierarchy:

Exception Hierarchy

How to handle Exception?

There is a process to handle exception. Most used and effective method to handle exception in java is “try-catch “method.

Try-catch method is used to handle exception. Keyword try and catch is used in method declaration to check exception. Then throw is keyword to use to handle exception.

Throws checked exception is then handled by the programmer to handle it by self or pass the exception.

Try catch method:

Try catch method

Try block

The try block includes a series of statements where might be an exception. A try block is often followed by a catch block, which manages the exception in the corresponding attempt row. Catch blocks or finally block or both must be followed by a try block.

Catch block

A catch block is where the exceptions are treated, and this block will obey the try rule. A single try block may be associated with several catch blocks. Within different sets of catches you can find various exceptions. When an exception occurs in the try block, it executes the corresponding catch block which handles that exception.


Finally clause is attached to the try catch block.The code within the finally clause will still be executed, even if the try or catch block throws an exception from inside.If your code has a return statement inside the try or catch block, it will execute the code inside the final block before returning from the method.

Finally Clause

Leave a Reply