Back to Blog
Interview QA
April 30, 2026
10 min read
1,995 words

Checked vs unchecked exceptions — what do you recommend in APIs?

Why This Matters The Question You are designing an API and need to decide whether to use checked or unchecked exceptions. Which approach would you recommend, and why? Short Answer …

Checked vs unchecked exceptions — what do you recommend in APIs?

Why This Matters

The Question

You are designing an API and need to decide whether to use checked or unchecked exceptions. Which approach would you recommend, and why?

Short Answer

In API design, it's recommended to use checked exceptions for recoverable errors that the caller can handle gracefully, such as network issues or file-related problems, and unchecked exceptions for programming errors that should never occur at runtime due to incorrect code or invalid input.

Model Answer

What are checked and unchecked exceptions?

In Java, checked exceptions (e.g., IOException, SQLException, FileNotFoundException) are exceptions that must be declared in a method's throws clause or caught within the method itself. They represent conditions that can occur during program execution but may still be handled by the caller. On the other hand, unchecked exceptions (e.g., NullPointerException, IllegalArgumentException) do not need to be declared or caught and are typically thrown when a programming or logical error occurs in the code, such as accessing an array index out of bounds or passing null to a method that requires an object.

Why use checked exceptions in APIs?

Checked exceptions help developers write more robust and maintainable code by requiring them to handle potential errors explicitly. This ensures that the caller is aware of the error and can take appropriate action, such as retrying the operation, providing alternative input, or returning an error message to the user. By making the caller responsible for handling checked exceptions, the API remains flexible and reusable across different contexts.

When do unchecked exceptions break?

Unchecked exceptions are typically thrown when a programming or logical error occurs in the code, such as accessing an array index out of bounds or passing null to a method that requires an object. If unchecked exceptions are not handled properly within the API, they can lead to runtime failures and difficult-to-debug issues for the caller.

Failure Modes and Edge Cases

  1. Unhandled Checked Exception: If a checked exception is thrown and not caught within the method or declared in the method's throws clause, it will propagate up the call stack until it is either handled or the program terminates with an unhandled exception error. This can lead to unexpected behavior and may cause the API to become unusable for the caller.
  2. Overuse of Checked Exceptions: Overusing checked exceptions for programming errors can result in excessive amounts of boilerplate code and make the API more difficult to maintain. It is essential to strike a balance between robust error handling and maintainable code by using checked exceptions for recoverable errors and unchecked exceptions for programming errors.
  3. Under-documentation: Inadequate documentation of exception handling within the API can lead to confusion for developers who are trying to understand how to handle exceptions effectively. It is crucial to document each exception that your API throws, including its purpose and any suggested handling strategies.
  4. Meaningless Error Messages: Providing unhelpful or misleading error messages can make it difficult for the caller to determine the cause of an issue and may result in unnecessary debugging efforts. It is essential to provide meaningful error messages that help the caller understand what went wrong and how to fix the problem.
  5. Test Cases: Thorough testing with a variety of inputs and edge cases can help ensure that your API handles exceptions effectively and provides useful feedback to the caller. This includes testing for different network conditions, file existence, input validation, and boundary cases.
  6. Exception Propagation: Proper handling of exception propagation is crucial in API design. If an exception occurs within a method, it should be either caught and handled or declared in the method's throws clause to ensure that the caller is aware of the potential error.
  7. Recovery Strategies: Designing appropriate recovery strategies for checked exceptions can help minimize the impact on the API's functionality. This may include retrying the operation, providing alternative input, or returning an error message to the user.
  8. Exception Hierarchy: Organizing exceptions within a hierarchy can help developers understand their relationship and provide a more structured approach to handling exceptions. The Exception class serves as the base class for all exceptions in Java.
  9. Exception Subclassing: Creating custom exception subclasses can help provide more specific error information and improve the maintainability of the API. This can be particularly useful when dealing with domain-specific errors.
  10. Exception Suppression: Exception suppression, or swallowing exceptions without handling them, should generally be avoided in API design as it can lead to unhandled exceptions and difficult-to-debug issues for the caller. However, in some cases, suppressing certain exceptions may be necessary to maintain the API's functionality, but it is essential to document this behavior clearly.

Q1: What happens if a checked exception is not caught within the method?

Answer: If a checked exception is not caught within the method, it must be declared in the method's throws clause. This means that the responsibility of handling the exception is passed up the call stack to the caller or to an exception handler further up the chain. If no one catches the exception, the program will terminate with an unhandled exception error.

Q2: Can you provide an example of a situation where it might be appropriate to use an unchecked exception?

Answer: An example of when it might be appropriate to use an unchecked exception is when the method's contract is violated by the caller, such as passing null where an object is expected. In this case, throwing an IllegalArgumentException or another unchecked exception helps indicate that the caller has made a programming error and should correct their code before continuing.

Q3: What are some common mistakes when using exceptions in API design?

Answer: Common mistakes include overusing checked exceptions for programming errors, under-documenting exception handling within the API, and not providing meaningful error messages to the caller. It's important to strike a balance between robust error handling and maintainable code by using checked exceptions for recoverable errors and unchecked exceptions for programming errors.

Q4: How do you determine whether a programming error is recoverable or not?

Answer: To determine whether a programming error is recoverable, consider whether it can be handled gracefully by the caller without causing significant impact to the API's functionality. If the error can be handled effectively and does not compromise the integrity of the API, it may be appropriate to use a checked exception. On the other hand, if the error represents a programming or logical error that should never occur at runtime due to incorrect code or invalid input, it is better to use an unchecked exception.

Q5: What are some strategies for handling unhandled exceptions in your API?

Answer: Strategies for handling unhandled exceptions in your API include providing a default error message and logging the exception details for further analysis. Additionally, you can create a centralized exception handler that catches all unhandled exceptions and logs them to a central location for easier debugging and issue tracking.

Q6: How do you balance robustness and maintainability when using exceptions in API design?

Answer: Balancing robustness and maintainability when using exceptions in API design requires careful consideration of the types of errors that may occur and the caller's ability to handle them effectively. Use checked exceptions for recoverable errors that can be handled gracefully by the caller, and unchecked exceptions for programming errors that should never occur at runtime due to incorrect code or invalid input. Document each exception that your API throws, including its purpose and any suggested handling strategies, and test thoroughly with a variety of inputs and edge cases to ensure robust error handling without compromising maintainability.

Follow-Up Q And A

Q1: What are some examples of recoverable errors that might warrant the use of checked exceptions?

Answer: Examples of recoverable errors that might warrant the use of checked exceptions include network issues (e.g., connection timeouts, socket errors), file-related problems (e.g., file not found, permission denied), and resource exhaustion (e.g., out of memory error). These types of errors can often be handled gracefully by the caller by retrying the operation, providing alternative input, or returning an error message to the user.

Q2: What are some examples of programming errors that might warrant the use of unchecked exceptions?

Answer: Examples of programming errors that might warrant the use of unchecked exceptions include accessing an array index out of bounds, passing null where an object is expected, or using a method with incorrect arguments. These types of errors should not occur at runtime due to incorrect code or invalid input and can be handled effectively by the caller if they are aware of the issue.

Q3: How do you determine whether a programming error is recoverable or not?

Answer: To determine whether a programming error is recoverable, consider whether it can be handled gracefully by the caller without causing significant impact to the API's functionality. If the error can be handled effectively and does not compromise the integrity of the API, it may be appropriate to use a checked exception. On the other hand, if the error represents a programming or logical error that should never occur at runtime due to incorrect code or invalid input, it is better to use an unchecked exception.

Q4: What are some strategies for handling unhandled exceptions in your API?

Answer: Strategies for handling unhandled exceptions in your API include providing a default error message and logging the exception details for further analysis. Additionally, you can create a centralized exception handler that catches all unhandled exceptions and logs them to a central location for easier debugging and issue tracking.

Q5: How do you balance robustness and maintainability when using exceptions in API design?

Answer: Balancing robustness and maintainability when using exceptions in API design requires careful consideration of the types of errors that may occur and the caller's ability to handle them effectively. Use checked exceptions for recoverable errors that can be handled gracefully by the caller, and unchecked exceptions for programming errors that should never occur at runtime due to incorrect code or invalid input. Document each exception that your API throws, including its purpose and any suggested handling strategies, and test thoroughly with a variety of inputs and edge cases to ensure robust error handling without compromising maintainability.

Common Mistakes

  1. Overusing checked exceptions for programming errors: This can result in excessive amounts of boilerplate code and make the API more difficult to maintain.
  2. Under-documenting exception handling within the API: Inadequate documentation can lead to confusion for developers who are trying to understand how to handle exceptions effectively.
  3. Not providing meaningful error messages to the caller: Unhelpful or misleading error messages can make it difficult for the caller to determine the cause of an issue and may result in unnecessary debugging efforts.
  4. Ignoring edge cases: Failing to test your API thoroughly with a variety of inputs and edge cases can lead to unexpected behavior and unhandled exceptions.
  5. Failing to log exception details: Proper logging of exception details can help developers identify and fix issues more quickly, reducing the impact on users.
  6. Inconsistent exception usage: Using a mix of checked and unchecked exceptions without a clear rationale can make it difficult for other developers to understand the API's error handling strategy.
  7. Not providing enough context in error messages: Error messages should provide enough information for the caller to understand what went wrong and how to fix the problem, including relevant details about the input and the operation being performed.
  8. Overcomplicating exception handling: Exception handling should be kept simple and easy to understand, avoiding unnecessary complexity that can make it difficult for developers to quickly identify and address issues.
  9. Not considering the user experience: Exception handling should prioritize providing a clear and helpful error message to the user, rather than simply logging the exception details without providing any information about what went wrong or how to fix it.
  10. Not providing multiple ways to handle exceptions: Providing multiple options for handling exceptions, such as retrying the operation, returning an error code, or returning an error object, can help developers choose the approach that best fits their needs and use case.
Tags:Interview QATutorialGuide
X

Written by XQA Team

Our team of experts delivers insights on technology, business, and design. We are dedicated to helping you build better products and scale your business.