Skip to main content

Exception Handling Interview Questions - Part 2

This guide explores the flow of exceptions through application layers and the concept of chaining exceptions for better debugging.

1. What is Exception Propagation?โ€‹

Exception propagation is the process where an exception is dropped from the current method and sent to the caller if it is not caught.

  • The Flow: Database Layer \rightarrow DAO Layer \rightarrow Service Layer \rightarrow Controller \rightarrow User Interface (Front-end).
  • The Risk: If no layer handles the exception, it propagates to the JVM, which often sends an "Internal Server Error" (HTTP 500) to the user. This is a poor user experience because it hides the real cause of the error.

2. Best Practice: Layered Exception Handlingโ€‹

Instead of letting raw exceptions reach the user, you should catch them at the Controller level (at a minimum) and wrap them in a user-friendly response.

  • Example: If a NoSuchElementException occurs because an ID doesn't exist, the Controller should catch it and return an EmployeeNotFoundException with a "Bad Request" (HTTP 400) status.

3. What are Chained Exceptions?โ€‹

Chained exceptions allow you to relate one exception to another. This helps developers understand the "root cause" of an issue.

  • initCause(Throwable cause): This method associates a specific underlying cause with the current exception.
  • getCause(): This method retrieves the original exception that caused the current one.

Real-World Example:โ€‹

If a user tries to access admin data, a Business Logic Exception might be thrown. By using chained exceptions, you can specify that the root cause was a Permission Denied Data Access Exception.

4. Why use Custom Exceptions?โ€‹

Custom exceptions allow you to add specific metadata to an error, such as:

  • Error Code: A unique ID for internal tracking.
  • Error Message: A descriptive string for the front-end user.
  • Timestamp: When the error occurred.

5. Coding Scenario: Handled vs. Unhandledโ€‹

Unhandled:

// Raw Exception propagates directly to user
public Employee getEmp(Long id) {
return repo.findById(id).get();
}