65 Java Exception Handling Interview Questions for 2024

Here, we have listed the most important exception handling interview questions in Java with the best possible answers for 2024. These interview questions are generally asked in Java interviews by the interviewer.

If you nicely prepare answers to these exception handling interview questions in Java then definitely, you can easily answer questions asked by the interviewer. So, let’s prepare for it.

Java Exception Handling Interview Questions Answers


1. What is Exception in Java?

Ans: An exception is an error that affects the normal execution of program. If an exception is not handled, the program abruptly terminates.

2. What is the super or base class of all exceptions in Java?

Ans: Exception is the superclass of all exceptions in Java.

3. What is the superclass for error and exception classes in Java?

Ans: Throwable is the superclass for error and exception classes in Java.

4. What are the types of exceptions in Java API?

Ans: There are two types of exceptions available in Java API. They are:

a. Predefined Exceptions (Built-in-Exceptions): Predefined exceptions are those exceptions that are already defined by Java system. These exceptions are also called built-in-exceptions.

The built-in-exception is further divided into two categories: checked and unchecked exceptions.

b. Custom Exceptions: Custom exceptions are those exceptions that are created by users or programmers according to their own needs. The custom exceptions are also called user-defined exceptions that are created by extending the exception class.

5. Why an exception occurs in the program?

Ans: There can be many reasons that might generate an exception in a Java program.

  • Opening a non-existing file in your program.
  • Reading a file from a disk but the file does exist there.
  • Writing data to a disk but the disk is full or unformatted.
  • When the program asks for user input and the user enters invalid data.
  • When a user attempts to divide an integer value by zero, an exception occurs.
  • When a data stream is in an invalid format, etc.

6. What is Exception handling in Java?

Ans: The mechanism of handling unexpected errors in a java program is called exception handling. It is a powerful mechanism to handle runtime errors, ClassNotFoundException, FileNotFoundException, IOException, etc. so that the normal execution flow of the program can be maintained.

7. What is exception handler in Java?

Ans: The code that catches the exception thrown by JVM is called exception handler in Java. In other words, an exception handler is a block of code that performs an action when an exception occurs in a program.


8. What is the advantage of using exception handling in Java?

Ans: There are several advantages of using exception handling in java. They are:

  • The main advantage of exception handling technique is to maintain the normal flow of the program.
  • It provides flexibility in handling situations of errors.
  • It allows us to define a user-friendly message to handle the exception.
  • It helps to separate “Error-Handling code” from “Regular code.”

9. Which of the following statements will throw an exception?
a. System.out.println(1/0);
b. System.out.println(2.0/0);

Ans: a will throw an exception named ArithmeticException. The second statement will give output infinity.

10. Which exception may be thrown if the given code is executed?

a.

public class Test {
public static void main(String[] args) 
{
   int[ ] list = new int[4];
   System.out.println(list[4]);
  }
}

Ans: ArrayIndexOutOfBoundsException will be thrown if the above code is executed.

b.

public class Test {
public static void main(String[] args) 
{
   int a = 20;
   int b = 30;
   int c = 10;
   int x = (a * b)/(a - b + c);
   System.out.println("Result: " +x);
  }
}

Ans: ArithmeticException will be thrown if code is executed.

11. Explain the Java exception hierarchy.

Ans: The hierarchy of exception in Java is shown in the below diagram.

Exception handling in Java interview questions

Throwable class is the parent class of all exception types. It is an immediate subclass of the Object class. Below Throwable class, there are two subclasses (two child objects) Error and Exception.

Error: Error class is the subclass of Throwable class and a superclass of all the runtime error classes. It terminates the program if there is a problem related to a system or resources (JVM).


Exception: An exception is an abnormal condition that is caused by runtime error in the program. It is the superclass of all exceptions in Java. It is further divided into checked and unchecked (runtime) exceptions.

The exception class hierarchy has been shown in the below figure.

Java exception handling interview questions for exception class hierarchy

Checked exceptions are those exceptions that are checked by Java compiler at compilation. A list of some important checked exceptions are given below:

  • ClassNotFoundException
  • InterruptedException
  • InstantiationException
  • IOException
  • SQLException
  • IllegalAccessException
  • FileNotFoundException, etc

Runtime exceptions are those exceptions that are checked by JVM at runtime. Some important examples of runtime exceptions are given below:

  • ArithmeticException
  • ClassCastException
  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • NegativeArraySizeException
  • ArrayStoreException
  • IllegalThreadStateException
  • SecurityException, etc.

12. What does JVM do when an exception occurs in a program?

Ans: When JVM faces an exception in a program, it creates an exception object and throws it to inform us that an error has occurred. If the exception object is not caught and handled properly, JVM will display an error message and will terminate the rest of the program abnormally.

13. How do we catch an exception?

Ans: We can catch an exception in either of two ways. They are:

  • By try-catch block
  • By using throws clause

14. What is throwing an exception in Java?

Ans: When an exception occurs inside a method in java program, the method in which exception has occurred creates an exception object (i.e, an object of exception class) internally with the help of JVM and hands it over to the java runtime system (JVM). This process is called throwing an exception in java.

15. What is catching an exception in Java?

Ans: The process of finding a handler by JVM to handle thrown exception is called catching an exception.

16. What will happen to exception object after exception handling is done?

Ans: Once exception handling is done, the exception object will be garbage collected.

17. What is the difference between checked and unchecked exceptions in Java?

Ans: Refer to answer to question 11.

18. How will you handle the checked exception?
Or What are the different ways to handle checked exceptions?

Ans: A checked exception can be handled either by using try and catch block or by using throws clause in the method declaration. If not handles properly, it will give a compile-time error.

19. Which exception class can you use in the catch block to handle both checked and unchecked exceptions?

Ans: Exception class

20. Can we throw checked exceptions from the static block?

Ans: We cannot throw because there is no specific place to catch it and it is called only once.

21. Do checked exceptions occur at compile time?

Ans: Checked exceptions do not occur at compile-time. All exceptions always occur at runtime only but some exceptions are detected at compile-time and some other at runtime.

22. Are compile-time errors exceptions?

Ans: Compile-time errors are not exceptions. They come under errors. In Java, only runtime errors come under exceptions.

23. Does Java compiler check Runtime exceptions at compilation?

Ans: Java compiler does not check runtime exception at compile time whether the programmer handles them or not.

24. What happens when runtime exception occurs in a program?

Ans: When a runtime exception occurs in a method and the programmer does not handle it, JVM terminates program without the execution of the rest of the code.

25. What are the keywords to handle an exception in Java?

Ans: Java provides five essential keywords to handle an exception. They are:

  • try
  • catch
  • finally
  • throw
  • throws

These keywords can be used to handle exceptions properly.

26. What happens when an exception is thrown by the main method?

Ans: When an exception is thrown by the main() method, JVM terminates the program and prints the exception message and stack trace in system console.

27. What is try block in Java?

Ans: A try is a block of code or statements that might throw an exception. That’s why a try block is also known as exception generated block. An exception generated code (risky code) must be placed within a try block.

28. What is catch block in Java?

Ans: A catch is a block of code that handles the exception thrown by the try block. That’s why it is also known as exception handler block.

Read complete tutorial: Java try catch block

29. Do we have to always put a catch block after a try block?

Ans: No, we do not always need to put a catch block after a try block.

30. What are the three possible forms of try block?

Ans: There are three possible forms of try block that are:

  • try-catch
  • try-finally
  • try-catch-finally

31. Can we write statements between try block and catch block?

Ans: A catch block must be followed by try block. We cannot write a statement between the end of try block and the beginning of catch block.

32. What is a nested try block in Java?

Ans: A try block within another try block is called nested try block.

33. Assume that statement 2 arises an exception in the following statements. The exception object created is matched with argument of the catch block.

try 
{ 
  statement 1; 
  statement 2; 
  statement 3; 
} 
catch(exception_class var) 
{ 
  statement 4; 
} 
statement 5;

Which of the following statements is true?
a. statement 1 will be executed normally.
b. statement 4 inside catch block will be executed.
c. After executing statement 4, statement 3 in try block will be executed.
d. After executing statement 3, statement 5 will be executed.

Ans: a, b.

34. In question number 31, suppose the exception object created is not matched with argument of the catch block.

Which of the following statements is true?
a. statement 1 will be executed normally.
b. Program will be terminated abnormally after executing statement 5.
c. Program will be terminated abnormally and the rest of code will not execute.
d. Program will be terminated normally.

Ans: a, c.

35. In question number 31, suppose the exception arises in statement 4.

Which of the following statements is true?
a. statements 1, 2, 3 will be executed normally.
b. statement 5 will be executed.
c. Program will be terminated abnormally after execution of statement 5.
d. Program will be terminated normally.

Ans: a, b, d.

36. Which of the following statements are true?
a. Java try-catch block must be within a method.
b. A try block can also be used without a catch or finally block.
c. A finally block cannot come before catch block.
d. A catch block cannot be empty.

Ans: a, c.

37. Which of the following are checked exceptions?
a. ClassNotFoundException, InstantiationException, IllegalAccessException
b. ClassNotFoundException, InstantiationException, ClassCastException
c. ArrayIndexOutOfBoundsException, NegativeArraySizeException, ArrayStoreException
d. NegativeArraySizeException, SQLException

Ans: a.

38. Which of the following is RuntimeException?
a. IOException
b. IllegalAccessException
c. ArrayStoreException
d. None of these.

Ans: c.

39. Which exception will be thrown by the following code?

public 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 (ArrayIndexOutOfBoundsException ai) {
    System.out.println("ArrayIndexOutOfBoundsException");
  }
 catch (RuntimeException ex) {
    System.out.println("RuntimeException");
 }
 }
}

Ans: ArrayIndexOutOfBoundsException

40. What is unreachable catch block error in Java?

Ans: Refer to this tutorial: Multiple catch block in Java

Please share your love