Bird
0
0

Identify the error in this async method that uses Future and explain why exceptions are not handled properly:

medium📝 Debug Q14 of 15
Spring Boot - Async Processing
Identify the error in this async method that uses Future and explain why exceptions are not handled properly:
@Async
public Future loadData() {
  throw new RuntimeException("Failed");
}
AFuture cannot be used with @Async methods
BMethod should not be annotated with @Async when returning Future
CThrowing exception directly in async method causes it to be lost; should return Future with exception
DRuntimeException must be caught inside the method before throwing
Step-by-Step Solution
Solution:
  1. Step 1: Understand async method exception handling

    Exceptions thrown directly in async methods returning Future are not propagated properly.
  2. Step 2: Proper way to handle exceptions in Future

    Return a Future that completes exceptionally instead of throwing directly.
  3. Final Answer:

    Throwing exception directly in async method causes it to be lost; should return Future with exception -> Option C
  4. Quick Check:

    Throwing exceptions directly in async Future methods is incorrect [OK]
Quick Trick: Return Future with exception, don't throw directly in async methods [OK]
Common Mistakes:
  • Throwing exceptions directly in async methods returning Future
  • Ignoring exception handling in async methods
  • Misusing @Async annotation with incompatible return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes