Using @ExceptionHandler in standalone command handler

I am implementing a class that will handle commands, but which is not an aggregate:

@Component
public class AxonCommandHandler {

    @CommandHandler
    public void handle(Rename command) throws SomeApplicationException {
        ....
    }
}

This works as expected - the command handler method is invoked, when a Rename command is sent using the command gateway.

I would now like to add an exception handler to AxonCommandHandler, like this:

    @ExceptionHandler(resultType = SomeApplicationException.class)
    public void handleException() {
        ...
    }

This does not seems to work however, as the exception handler method is not called, if a SomeApplicationException is thrown.

Am I doing something wrong? Or does the exception handler mechanism only work from an aggregate?

The ExceptionHandler annotated method, will effectively become an MessageHandlerInterceptor. In most cases like with aggregates and event handlers, it works without additional configuration. There is an issue and pr to add it to Saga’s as well. That might be useful to also make it work in this case.

I just have to be sure: Can you confirm that I am not doing something wrong and that the ExceptionHandler-annotation has no effect in the situation sketched above?

Indeed, there are at least three options, don’t use the exception handler in this case, turn it into an aggregate, or do some of the configurations yourself.