Top Java Exception Handling Interview Questions and Answers

Exception handling is a crucial part of Java programming. Here's a list of the most frequently asked Java Exception Handling interview questions with concise answers to help you prepare for your next technical round.

1️⃣ What is an Exception in Java?

An exception is an event that disrupts the normal flow of the program. It's an object that represents an error.

2️⃣ What is the difference between Checked and Unchecked Exceptions?

  • Checked: Checked at compile-time (e.g., IOException, SQLException).
  • Unchecked: Checked at runtime (e.g., NullPointerException, ArithmeticException).

3️⃣ Explain try-catch block in Java

Used to catch and handle exceptions.


try {
   int x = 10 / 0;
} catch (ArithmeticException e) {
   System.out.println("Can't divide by zero");
}

4️⃣ What is the difference between throw and throws?

  • throw: Used to explicitly throw an exception.
  • throws: Declares exceptions a method might throw.

5️⃣ Can we catch multiple exceptions in one catch block?

Yes, using multi-catch (Java 7+):


try {
  // code
} catch (IOException | SQLException ex) {
  ex.printStackTrace();
}

6️⃣ What is finally block?

The finally block always executes, regardless of exception occurrence. It's used for cleanup operations.

7️⃣ What happens if exception is not caught?

It propagates up the call stack. If uncaught, the JVM terminates the program and prints stack trace.

8️⃣ Can we have try block without catch block?

Yes, but only if you use a finally block.

9️⃣ What is exception propagation?

Process of forwarding the exception to the calling method. Handled using method call stack.

🔟 How do you create a custom exception?


class MyException extends Exception {
   public MyException(String message) {
      super(message);
   }
}

1️⃣1️⃣ What is the base class for all exceptions?

java.lang.Throwable is the superclass of all exceptions and errors.

1️⃣2️⃣ Difference between Error and Exception?

  • Exception: Can be handled (e.g., FileNotFoundException).
  • Error: Cannot be handled (e.g., OutOfMemoryError).

1️⃣3️⃣ What is try-with-resources?

Introduced in Java 7, used for automatic resource management.


try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
   // use br
}

📌 Conclusion

Understanding how exceptions work and how to handle them properly is essential for building robust Java applications. Master these questions and you'll confidently handle any exception-related topic in interviews.