Exception Handling Program in Java for 2026

5. What exception will be thrown when the following program is run?

class Test {
public static void main(String[] args)
{ 
   try { 
      int[] list = new int[5];
      System.out.println(list[5]);
   } 
   catch(StringIndexOutOfBoundsException e) 
   {
      try {
         System.out.println("ABC");	
         int x = 10/0;
      }
      catch(ArithmeticException ae) { } 
   }
   System.out.println("ABC");  
 }
}

Output: ArrayIndexOutOfBoundsException

6. What exception will be thrown when the program is run?

class Test {
public static void main(String[] args)
{ 
    try {
       int[] list = new int[10];
       System.out.println("list[10] is " + list[10]);
    }
    catch (ArithmeticException ex) {
         System.out.println("ArithmeticException");
    }
    catch (RuntimeException ex) {
         System.out.println("RuntimeException");
    }
    catch (Exception ex) {
         System.out.println("Exception");
    } 
 }
}

Output: RuntimeException

7. What is displayed on the console when the following program is run?

a)

class Test {
public static void main(String[] args)
{ 
   try {
      method();
      System.out.println("After the method call");
   }
   catch (StringIndexOutOfBoundsException se) {
       System.out.println("StringIndexOutOfBoundsException");
   }
   catch (RuntimeException ex) {
       System.out.println("RuntimeException");
   }
   catch (Exception e) {
       System.out.println("Exception");
   }
}
static void method() throws Exception {
    String str = "Hello";
    char ch = str.charAt(5);
    System.out.println(ch);
 }
}

Output: StringIndexOutOfBoundsException

b)

class Test {
public static void main(String[] args)
{ 
    try {
       method();
       System.out.println("I am in try block");
    }
    catch (RuntimeException ex) {
         System.out.println("RuntimeException in main");
    }
    catch (Exception ex) {
         System.out.println("Exception in main");
    }
}
static void method() throws Exception {
try {
   int a[] = {10, 20, 30, 40};
   a[10] = 5;
}
catch (ArrayIndexOutOfBoundsException ae) {
    System.out.println("ArrayIndexOutOfBoundsException in method()");
}
catch (RuntimeException re) {
    System.out.println("RuntimeException in method()");
}
catch (Exception e) {
    System.out.println("Exception in method()");
}
 }
}

Output: ArrayIndexOutOfBoundsException in method(), I am in try block

c)

class Test {
public static void main(String[] args)
{ 
    try {
       method();
       System.out.println("I am in try block");
    }
    catch(Exception e) {
        System.out.println("I am in catch block");	
    }
    finally {
        System.out.println("I am in finally block");	
    }
}
static void method() throws Exception {
try {
   int a[] = {10, 20, 30, 40};
   a[4] = 20;
   System.out.println(a[4]);
 
   try {
      int x = 1/0;
      System.out.println(x);
   }
   catch(ArithmeticException ae) {
      System.out.println("I am in inner catch block");  
    }
}
catch (ArrayIndexOutOfBoundsException ae) {
     System.out.println("I am in outer catch block");
  }
 }
}

Output: I am in outer catch block, I am in try block, I am in finally block

d)

class Test {
public static void main(String[] args)
{ 
    try {
       method();
       System.out.println("I am in try block");
       int a = 1/0;
    }
    catch(Exception e) {
        System.out.println("I am in catch block");	
    }
    System.out.println("I am in main method");
}
static void method() throws Exception {
try {
   int a[] = {10, 20, 30, 40};
   a[3] = 20;
   System.out.println(a[3]);
 
   try {
      int x = 1/0;
      System.out.println(x);
   }
   catch(ArithmeticException ae) {
      System.out.println("I am in inner catch block");  
   }
}
catch (ArrayIndexOutOfBoundsException ae) {
     System.out.println("I am in outer catch block");
  }
 }
}

Output: 20, I am in inner catch block, I am in try block, I am in catch block, I am in main method

e)

class Test {
public static void main(String[] args) throws Exception
{ 
   try {
      method();
   }
   finally {
      System.out.println("I am in finally block");	
   }
}
static void method() throws Exception {
try {
   int[]  list = new int[5];
   System.out.println(list[0]);

   try {
      System.out.println(list[5]);
   }
   catch(ArithmeticException ae) {
        System.out.println("I am in inner catch block");  
   }
}
catch (ArrayIndexOutOfBoundsException ae) {
    System.out.println("I am in outer catch block");
  }
 }
}

Output: 0, I am in outer catch block, I am in finally block

8. What will be the output of the program when an exception occurred in try block?

class Test {
public static void main(String[] args) throws Exception
{ 
    try {
       System.out.println("ABC");
       int x = 1/0;
       System.out.println("PQR");

       try {
          System.out.println("XYZ");	
       }
       catch(ArithmeticException ae) {
           System.out.println("I am in inner catch block");  
       }
    }
    catch (ArrayIndexOutOfBoundsException ae) {
         System.out.println("I am in outer catch block");
    }
  }
}

Output: ABC, Exception in thread “main” java.lang.ArithmeticException: / by zero.

9. What will be the output when the following program has complied?

class Test {
public static void main(String[] args) throws Exception
{ 
    try {
       System.out.println("ABC");	
       int a  = 5, b = 10, c = 5;
       a += 5;
       b -= a + c;
       int x = (a + b)/(b + c);
       System.out.println(x);
       System.exit(0);
    }
    catch(ArithmeticException ae) {
        System.out.println("PQR");	
    }
    finally {
        System.out.println("XYZ");	
    }
 }
}

Output: ABC, PQR, XYZ

10. What will be the output of the program when System.exit(0); statement is used just above an exception in try block?

class Test {
public static void main(String[] args) throws Exception
{ 
    try {
       System.out.println("ABC");	
       System.exit(0);
       int x = 1 / 0;
    }
    catch(ArithmeticException ae) {
        System.out.println("PQR");	
    }
    finally {
        System.out.println("XYZ");	
    }
 }
}

Output: ABC

11. Will finally block be executed in the below program? What will be the output?

class Test {
public static void main(String[] args) throws Exception
{ 
   try {
      System.out.println("ABC");	
      int x = 1 / 0;
   }
   catch(ArithmeticException ae) {
      System.out.println("PQR");
      System.exit(0);
   }
   finally {
       System.out.println("XYZ");	
   }
 }
}

Output: No, finally block will not be executed.

    ABC, PQR

12. Will the program be successfully compiled?

class Test {
   int m1(){
      try {
         System.out.println("ABC");
         return 50;
      }
      catch(Exception e) {
          System.out.println("I am in catch block");
      }
   }	
public static void main(String[] args) { 
    Test t = new Test();
    t.m1();
 }
}

Ans: No, Compile time error.

13. What will be displayed on the console when the following program is run?

a)

class Test {
  int m1() {
       try {
          System.out.println("ABC");
          return 50;
       }
       catch(Exception e) {
           System.out.println("I am in catch block");
       }
     return 10;
  }	
public static void main(String[] args)
{ 
   Test t = new Test();
   System.out.println(t.m1());
 }
}

Output: ABC, 50

b)

class Test {
int m1() 
{
    try {
       System.out.println("ABC");
       int x = 1/0;
       return 50;
    }
    catch(ArithmeticException ae)
    {
       System.out.println("I am in catch block");
    }
   return 10;
}	
public static void main(String[] args)
{ 
    Test t = new Test();
    System.out.println(t.m1());
 }
}

Output: ABC, I am in catch block, 10

c)

class Test {
int m1()
{
    try {
       System.out.println("ABC");
       int x = 1/0;
       return 50;
    }
    catch(ArithmeticException ae) {
        System.out.println("I am in catch block");
        return 40;
    }
    finally {
        return 60;
    }
}	
public static void main(String[] args)
{ 
    Test t = new Test();
    System.out.println(t.m1());
 }
}

Output: ABC, I am in catch block, 60


In this tutorial, we have covered all kinds of exception handling programs in Java with output for interview purpose. Hope that you will have solved these Java exception handling interview program questions and enjoyed them.
Thanks for reading!!!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.