0% found this document useful (0 votes)
6 views

Java Exception Handling Interview Questions

Uploaded by

Nithin Kudupudi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Exception Handling Interview Questions

Uploaded by

Nithin Kudupudi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Java Exception Handling Interview Questions and Answers

1. What is the difference between checked and unchecked exceptions?


Answer:
- Checked exceptions must be declared in the `throws` clause of a method or caught in a `try-catch` block.
- Unchecked exceptions are runtime exceptions that do not need to be declared in the `throws` clause. The

2. How does exception propagation work in Java?


Answer:
Exception propagation in Java follows the call stack:
- If an exception occurs in a method and is not handled, it propagates to the caller method.
- This continues until it reaches the `main()` method. If not handled, the JVM terminates the program.

3. What happens if a `finally` block has a `return` statement?


Answer:
If a `finally` block contains a `return` statement, it overrides any return from the `try` or `catch` block.

4. Can we have multiple `catch` blocks for a single `try` block?


Answer:
Yes, Java allows multiple `catch` blocks, each handling a specific exception type.

5. Can a `catch` block handle multiple exceptions in Java 7+?


Answer:
Yes, since Java 7, a single `catch` block can handle multiple exceptions using the `|` (pipe) operator.

6. What happens if an exception occurs in the `finally` block?


Answer:
If an exception occurs in the `finally` block, it can override any previously thrown exception.

7. Can we catch `Throwable` instead of `Exception`?


Answer:
Yes, we can catch `Throwable`, but it is not recommended because `Throwable` includes `Error` (like `OutO

8. What is the difference between `throw` and `throws`?


Answer:
- `throw` is used to explicitly throw an exception.
- `throws` is used in method signatures to declare exceptions that a method might throw.
9. How does Java handle suppressed exceptions in `try-with-resources`?
Answer:
Java 7 introduced try-with-resources, which automatically closes resources and suppresses exceptions from

10. What is `CustomException` and when should you use it?


Answer:
A custom exception is a user-defined exception class that extends `Exception` or `RuntimeException`.
Use it when existing exceptions do not fit a specific business scenario.

11. What are `InterruptedException` and its importance in multi-threading?


Answer:
`InterruptedException` occurs when a thread is waiting, sleeping, or blocked and another thread interrupts

You might also like