Exception Handling in Java

In this tutorial, we will learn about an Introduction to Exception Handling in Java. According to the dictionary, the word exception means abnormal and Here Exception handling means to solve abnormal situations which occur while the execution of code.

During the execution of the program due to several reasons such as wrong input, or some programming mistakes, the program stops working and throw an error on screen which is known exception. Java has a concept called Exception Handling to handle these exceptions.

Example Program:

public class Example{
  public static void main(String args[]){
    System.out.println("Ans of 10/2 ="+(10/2));
    System.out.println("Ans of 12/3 ="+(12/3));
    System.out.println("Ans of 5/0 ="+(5/0));
    System.out.println("Ans of 24/6 ="+(24/6));
    System.out.println("Ans of 8/2 ="+(8/2));
  }
}

Output:

Ans of 10/2 =5
Ans of 10/2 =4
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Example.main(Example.java:5)}

As in the above example, you can see our code was working perfectly till the 4th line. But when it reached to 5th line and we tried to divide 5 by 0. We got an exception(In Java / by Zero is an Exception) and our program stropped to working immediately as two can see there were two more lines(line number 6th and 7th) which were not executed because of Exception.

In java, there are various inbuilt classes available in Java Standard Library which helps us to work with exception class. The parent class of all the exception class is java.lang.Exception Class

Types of Exception:

Java has two kinds of exception which an occur in Java programs. Types of Exception are as follow:

  • Checked Exception
  • Unchecked Exception

Checked Exception:

The checked exceptions are those exceptions which can be caught on compilation time by the compiler. These kinds of exceptions can not be ignored by the compiler. The programmer has to solve these exceptions first to run the program(without solving these checked exceptions program would not compile).

List of Checked Exceptions in Java:

  • ClassNotFoundException
  • NoSuchFieldException
  • NoSuchMethodException
  • InterruptedException
  • IOException
  • InvalidClassException
  • SQLException
  • any many more…

Example Program of Checked Exception in Java:

import java.io.File;
import java.io.FileReader;
public class Example{
   public static void main(String args[]) {		
      File file = new File("c://myfile.txt");
      FileReader fileReader = new FileReader(file); 
   }

Output:

Example.java:8: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
      FileReader fileReader = new FileReader(file); 

Unchecked Exception:

Unchecked Exceptions are those exceptions which can not be caught at the time of compilation of code. These exceptions only occur while the execution of code and stops the code from working.

Example Program of Unchecked Exception in Java:

public class Example {
   static String names[]={"ABC","XYZ","MNO"};
   public static void main(String args[]) {
      System.out.println(names[5]);
   }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
	at Example.main(Example.java:4)

List of Unhecked Exceptions in Java:

  • ArithmeticException
  • ArrayIndexOutOfBoundsException
  • EnumConstantNotPresentException
  • NegativeArraySizeException
  • NumberFormatException
  • NullPointerException
  • IllegalStateException
  • UnsupportedOperationException
  • and many more..

Exception Class Hierarchy:

The exception classes which we saw in list of checked and unchecked Exception classes are child classes of Exception class. Here is illustration to show hierarchy of all these exception classes.

Please continue with next tutorial the next tutorial we will learn how to handle exception in java using try catch block.

Spread the love
Scroll to Top
×