0
0
Spring Bootframework~20 mins

Exception handling in async in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when an exception is thrown inside an @Async method without a custom AsyncUncaughtExceptionHandler?
Consider a Spring Boot service method annotated with @Async that throws a runtime exception. What is the default behavior if no custom AsyncUncaughtExceptionHandler is configured?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    @Async
    public void asyncMethod() {
        throw new RuntimeException("Error in async method");
    }
}
AThe exception is silently ignored without any logging or notification.
BThe exception is propagated back to the caller thread and must be caught there.
CThe application crashes immediately due to the uncaught exception in the async thread.
DThe exception is logged by Spring's SimpleAsyncUncaughtExceptionHandler and does not propagate to the caller thread.
Attempts:
2 left
💡 Hint
Think about how Spring handles exceptions in void async methods by default.
📝 Syntax
intermediate
2:00remaining
Which method signature correctly allows exception handling for an async method returning a Future?
You want to handle exceptions thrown in an async method that returns a CompletableFuture<String>. Which method signature is correct to enable catching exceptions in the caller thread?
A
@Async
public CompletableFuture&lt;String&gt; asyncMethod() {
    return CompletableFuture.supplyAsync(() -&gt; {
        if(true) throw new RuntimeException("Error");
        return "Done";
    });
}
B
@Async
public void asyncMethod() throws Exception {
    throw new RuntimeException("Error");
}
C
@Async
public CompletableFuture&lt;String&gt; asyncMethod() {
    throw new RuntimeException("Error");
}
D
@Async
public String asyncMethod() {
    throw new RuntimeException("Error");
}
Attempts:
2 left
💡 Hint
Think about how to return a Future that can carry exceptions asynchronously.
🔧 Debug
advanced
2:00remaining
Why does this custom AsyncUncaughtExceptionHandler not log exceptions as expected?
Given the following custom AsyncUncaughtExceptionHandler, exceptions thrown in async void methods are not logged. What is the likely cause?
Spring Boot
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

@Component
public class CustomAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        // Missing logging implementation
    }
}
AThe method signature is incorrect and does not override the interface method.
BThe @Component annotation is missing, so Spring does not detect the handler.
CThe handler does not log or print the exception, so exceptions appear unlogged.
DThe handler must extend SimpleAsyncUncaughtExceptionHandler to work properly.
Attempts:
2 left
💡 Hint
Check what the handler does inside the overridden method.
🧠 Conceptual
advanced
2:00remaining
How can you globally configure a custom AsyncUncaughtExceptionHandler in Spring Boot?
Which approach correctly sets a global custom AsyncUncaughtExceptionHandler for all @Async methods in a Spring Boot application?
AImplement AsyncConfigurer in a @Configuration class and override getAsyncUncaughtExceptionHandler() to return your handler bean.
BAnnotate your handler class with @AsyncUncaughtExceptionHandler and Spring auto-detects it.
CRegister your handler as a @Bean of type AsyncUncaughtExceptionHandler in any @Configuration class.
DAdd @ExceptionHandler annotation on your handler class to catch async exceptions globally.
Attempts:
2 left
💡 Hint
Think about how Spring Boot allows configuring async behavior globally.
state_output
expert
3:00remaining
What is the output when an exception is thrown inside an async method returning CompletableFuture and handled with exceptionally()?
Consider this Spring Boot async method and caller code. What will be printed?
Spring Boot
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.concurrent.CompletableFuture;

@Service
public class AsyncService {
    @Async
    public CompletableFuture<String> asyncMethod() {
        return CompletableFuture.supplyAsync(() -> {
            throw new RuntimeException("Async error");
        });
    }
}

// Caller code
AsyncService service = new AsyncService();
service.asyncMethod()
    .exceptionally(ex -> {
        System.out.println("Caught: " + ex.getMessage());
        return "Recovered";
    })
    .thenAccept(result -> System.out.println("Result: " + result));
A
Caught: java.lang.RuntimeException: Async error
Result: Recovered
B
Caught: Async error
Result: Recovered
CNo output because exceptions in async are swallowed silently
DResult: Recovered
Attempts:
2 left
💡 Hint
Check how the exception message is accessed inside exceptionally().