Bird
0
0

Consider this async method in Spring Boot:

medium📝 component behavior Q4 of 15
Spring Boot - Async Processing
Consider this async method in Spring Boot:
@Async
public CompletableFuture getData() {
  if(true) throw new IllegalStateException("Failure");
  return CompletableFuture.completedFuture("Success");
}

What will the caller receive when invoking getData()?
AA <code>CompletableFuture</code> that completes exceptionally with <code>IllegalStateException</code>.
BA null value immediately due to the exception.
CA completed <code>CompletableFuture</code> with the string "Success".
DThe exception is caught and logged internally, returning an empty future.
Step-by-Step Solution
Solution:
  1. Step 1: Exception thrown before CompletableFuture creation

    The method throws an exception before returning any CompletableFuture.
  2. Step 2: Spring async behavior

    Spring wraps the thrown exception inside a CompletableFuture that completes exceptionally.
  3. Final Answer:

    The caller receives a CompletableFuture that completes exceptionally with IllegalStateException. -> Option A
  4. Quick Check:

    Exceptions thrown in async methods returning CompletableFuture complete the future exceptionally [OK]
Quick Trick: Exceptions in CompletableFuture async methods complete future exceptionally [OK]
Common Mistakes:
  • Expecting null or immediate exception instead of exceptional future.
  • Assuming the method returns a successful future despite the exception.
  • Believing exceptions are logged internally without affecting the future.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes