The Exception Handling in Java is one of the most powerful concept or methodology to handle the exceptions through which normal flow of the program is maintained or achieved.

In this page, we will learn what is Exception in Java and will also discuss different types of Exceptions with Exception class hierarchy.

Dictionary meaning of Exception is-

i.) a person or thing that is excluded from a general statement or does not follow a rule.”

ii.) an abnormal condition.

In Java Exception is an interrupt which stops the normal flow of execution of any program. In Java Exception is an object which is thrown at run time.

Problem without Exception Handling

  • statement 1;
  • statement 2;
  • statement 3;
  • statement 4;
  • statement 5;
  • statement 6; // exception occurs
  • statement 7;
  • statement 8;
  • statement 9;
  • statement 10;

NOTE: In the above scenario exception occurs at statement 6 which stops the normal flow of execution of the program and thus statement 7, 8, 9 & 10 is not executed which should be executed. In order to execute 7, 8, 9 & 10 exception handling is implemented so that normal flow of execution is maintained.

Types of Java Exceptions

Java Exception class Hierarchy
  1. Checked Exception
  2. Unchecked Exception
  3. Error

1. Checked Exception:

The classes which has been derived by Throwable class except RuntimeException and Error are known as checked exceptions. Example: IOException, SQLException, etc. Checked exception occurs at compile time.

2. Unchecked Exception:

The classes which are inherited by RuntimeException class is known as unchecked exceptions. It occurs at runtime. Example: ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, etc. They are not checked at compile time.

3. Error:

Error is a condition which cannot be recovered and are beyond the control of any programmer. It surely causes termination of the program abnormally. Example: OutOfMemoryError, VirtualMachineError, AssertionError etc.

The following are the 5 keywords that are used in Java for Exception Handling:

  1. try
  2. catch
  3. finally
  4. throw
  5. throws
KeywordDescription
tryThe “try” keyword is used as a block to place the code where exception may occurs. It must be followed by either catch block or finally block.
Note: try block cannot be used alone.
catchThe “catch” block is used to handle the exceptions that are generated in try block. Note: catch block must be preceded by try block and cannot be used alone.
finallyThe “finally” block is used to execute the important code of any program. Finally block is always executed whether exception is handled or not.
throwThe “throw” keyword is generally used to throw an exception.
It is used to define custom exception (user defined exception). One can throw checked or unchecked exception according to their need.
throwsThe “throws” keyword is generally used to declare an exception. It is always used with method signature and it is never used to throw an exception. Throws keyword is used to give a prior information to the programmer that there may occur an exception so it is better to handle the exception to maintain the normal flow of the program.

Problem without Exception Handling

Let’s go through the given program to see the importance of exception handling in java.

class ExceptionDemo
  {
  public static void main(String args[])
   {
  System.out.println("Hello students");
  System.out.println("Be serious in your studies");
  int a=50/0; // exception occurs
  System.out.println("Never give up");
  System.out.println("Labour hard");
    }
 }

Note: paste the above program in notepad/notepad++ and save the file as “ExceptionDemo.java”

To compile: javac ExceptionDemo.java

To run: java ExceptionDemo

Output:

Problem without Exception Handling

In the above program without exception handling rest of the statements are not executed after occurrence of exception on the statement “int a=50/0;” which can also be seen in the above given output. Therefore, to overcome this problem and to maintain the normal flow of execution of any program “Exception Handling” is needed.

To overcome the above problem Exception Handling is used with the help of try and catch block.

class ExceptionDemo
 {
 public static void main(String args[])
  {
 System.out.println("Hello students");
 System.out.println("Be serious in your studies");
 
 try
   {
   int a=50/0;
    }catch(Exception e)
       {
        System.out.println(e); 
        System.out.println("Exception Handled");
        }
 System.out.println("Never give up");
 System.out.println("Labour hard");
   }
}

Output:

Program with implementation of Exception Handling

Common Scenarios in which Exception Occurs are as under:

  • ArithmeticException occurs: If any number is divided by zero then it gives an ArithmeticException. For Example:
int a= 20/0; // ArithmeticException
  • NullPointerException occurs: If we have any null value in any variable and we try to perform any operation on that variable then it will result in NullPointerException. Let’s see an example:
String s=null;
System.out.println(s.length()); // NullPointerException 
  • ArrayIndexOutOfBoundsException occurs: If anyone inserts or stores the value into the wrong index in an array, then there occurs an ArrayIndexOutOfBoundsException. Let’s see the scenario through the given statament.
int a[]=new int[10];  
a[15]=20; // ArrayIndexOutOfBoundsException
  • NumberFormatException occurs: If anyone try to store value into the wrong formatting style, then there occurs a NumberFormatException. Suppose converting a string characters into digit will give NumberFormatException.
String s="ayushi";  
int i=Integer.parseInt(s);//NumberFormatException