When we're doing a refactoring, the rule is to avoid changes in observable behavior.In situations like this, such a rule leads immediately to the question of what behavior is observable.Obviously the throwing of the correct exception is something the outer program will observe - but to what extent do they care about the error message?The notification will eventually collect multiple errors and could summarize them together into a single message with something like class Notification…I've not mentioned the example domain so far, since I was just interested in the broad shape of the code.But as we explore the example further, I'll need to engage with the domain.A notification is an object that collects errors, each validation failure adds an error to the notification.
The booking request contains just two elements that we are validating here, the date of the performance and how many seats are being requested class Booking Request…
-- Dave Thomas and Andy Hunt An important consequence of this is that whether to use exceptions for a particular task is dependent on the context.
So, as the prags go on to say, reading from a file that isn't there may or may not be an exception depending on the circumstances.
If any of these checks fails, you throw an exception with an error message. Firstly I'm not happy with using exceptions for something like this.
Exceptions signal something outside the expected bounds of behavior of the code in question.