Why must an @EventSourcingHandler method never throw an exception for a business-rule reason?
By the time an @EventSourcingHandler method runs, the event it's reacting to represents something that has already, definitively happened and, in most real flows, is already durably stored in the event store. There's no remaining decision to make — that decision was made earlier, inside the @CommandHandler that chose to apply this event. If the event sourcing handler threw an exception, Axon would have no sensible way to recover: the event can't be un-stored, but the aggregate's in-memory state also can't be made to reflect it. Worse, since this same method also runs during a full replay of the aggregate's history, a thrown exception there wouldn't just fail once — it could make the aggregate unable to load at all going forward, since every future replay would hit the identical exception. All genuine validation belongs strictly in the command handler, before any event is ever applied.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response