Exception Handling in Java


An unexcepted unwanted event that disturb normal flow of Java program execution is known as an exception.

Exception Handling in Java

For Example-

  • File not found exception in Java
  • IOException

It is highly recomended to handel those exception so that normal flow of program execution continues as it is .

Exception handling doesn’t mean reparing an exception, we have to provide alternative way to continue program execution is a concept of exception handling.


In a Java program, we have to read data from a file that is located at another location. If the file is unavailable for some reason, then our Java program should not be terminated abnormally. We have to provide some alternative logic for this condition so that our program will keep running if the file is not available. This alternative logic is nothing but exception handling.

Default Exception Handling

If an exception occurs inside a Java method, the same method is responsible for the exception object. The exception object consists of the following information-

  1. Name of the Exception
  2. Description of Exception
  3. Location at which Exception occurs(stack trace)

After making the exception object method gives that object to the JVM.

Not JVM will check whether the method contains any exception handling code or not. If there is no code for exception handling in the method then JVM will terminate that method abnormally and remove the current entry(method) from the stack.

After that, JVM identifies the caller method and checks whether the caller method contains any exception handling code or not. If the Caller method doesn’t contain an exception handling code then JVM will terminate that caller method abnormally, and remove the caller method entry from the stack. The same procedure will continue until the main method and if there is no exception handling code in the main method then JVM will also terminate the main method abnormally and remove the corresponding entry(main) from the stack, after that JVM handover the responsibility of exception handling to the default exception handler which is part of JVM. The default exception handler prints exception information in the following format and terminates program execution abnormally.

Exception in thread “ABC” name of Exception: Description stack track.

For Example in the below code if we try to divide a number by zero then we will get following exception – “

Exception in thread “main” java. lang.ArithmeticException: / by zero
at Exception.ExceptionExample.Division(ExceptionExample.java:5)
at Exception.ExceptionExample.main(ExceptionExample.java:9)”.

package Exception;

public class ExceptionExample {
    public static void Division (int a, int b){
        System.out.println( a+ " /" + b + "= " + a/b);

    public static void main(String[] args) {
        Division(50, 0);
Default Exception Handling

Note-. In a program at least one method terminates abnormally then the program termination is abnormal, A Java program termination is normal termination if and only if all methods terminated normally.

Similar Java Tutorials

1 thought on “Exception Handling in Java”

Leave a Comment