Video in progress

Strong Middle 🌱 Spring

Where does an exception thrown in an @Async void method go, and how do you keep it under control?

It never reaches the caller — the caller has already moved on. For void methods the error lands in AsyncUncaughtExceptionHandler (by default it just logs). In a method returning CompletableFuture, the error is stored inside the future and someone has to check it.

📖 Answer

@Async
public void syncToCrm(long customerId) {
    crm.push(load(customerId)); // if the CRM is down — exception, but the user never knows
}

By default Spring just writes it to the log as ERROR via SimpleAsyncUncaughtExceptionHandler, and that's it. If nobody watches the logs, the data quietly disappears.

Your own handler:

@Configuration
@EnableAsync
class AsyncConfig implements AsyncConfigurer {
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (ex, method, params) -> {
            log.error("Async failed: {} {}", method.getName(), Arrays.toString(params), ex);
            failedTasks.increment(); // metric -> alert
        };
    }
}

With CompletableFuture:

crmSync.syncToCrm(id).exceptionally(ex -> { log.error("CRM sync failed", ex); return null; });

If nobody calls join()/exceptionally, the error is never seen at all.

For important work like money or syncing with an external system, fire-and-forget @Async isn't enough: the in-memory queue is lost on restart. Do that kind of work through a database-backed queue or a message broker, with retries and a dead letter queue.