p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional Java Development with the Spring Framework (http://p2p.wrox.com/forumdisplay.php?f=181)
-   -   Exception design on Boxoffice Sample Application? (http://p2p.wrox.com/showthread.php?t=35023)

dleal November 10th, 2005 05:13 PM

Exception design on Boxoffice Sample Application?
Dear Members,

Looking in to the BoxOffice sample Aplication the exception messages are written in "hard code", they don't use any ResourceBundle for that.

For example (From NotEnoughSeatsException):


public NotEnoughSeatsException(long classId, int available, int requested) {
        super(ERROR_CODE, "Requested " + requested + " seats in class " + classId + "; only " + available + " are free");
        this.classId = classId;
        this.requested = requested;
        this.available = available;

Every exception is designed on a similar way. There is no constructor with message as input argument.

How to implement a language independent way for defining the message exceptions?

In think in two possibilities:

1. Add a ResourceBundle attribute on the ApplicationException (base exception for all possible application exceptions)

2. Defining the exception constructors with a message attribute in order to be filled on places the exceptions will be thrown. With this solutions we need only a few exceptions, because ApplicationException with different message could apply for most cases (Probably a poor design strategy)

I am assuming the Application Exception police should consider that the exceptions are not only caught by the web side client, that is why each tier should be independ, so if I want also to run my application on console mode I would like that my exception will be treated property on language independ way (at least user exceptions)

So what is the purpose of the bean:


    <bean id="messageSource"        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">

Is is correct to desing an Exception policy creating dependence with such bean, how can we do that?, or it is better the solution I have propoused (1).

How can I unify this exception police using error-messages.properties from my web client side and also for throwing the exception could be caught on non web application.

If you take a look on BookSeatsController class from BoxOffice application, the exception are catch, in the form:


try {
            reservation = boxOffice.allocateSeats(request);
        } catch (RequestedSeatNotAvailableException e) {
            errors.reject(e.getErrorCode(), "Requested seats are not available");
        } catch (NotEnoughSeatsException e) {
            errors.reject(e.getErrorCode(), "Could not allocate the number of seats requested");

So were goes this message in english????, should it be language independent? For example the error code: "seatNotAvailable" is not used at other places on the source code and there is no information on the error-message.properties of such error code, so it is not used as resource key for getting the translatated message.

As far as I remember on the Expert one-on-one J2EE book there is comment about the user error message and detailed error message (goes for log files) the first one should be language independent and the second one (log file) could be in english for example. I don't see very clear how to implement that on language indepent way and also independently of tier.

Thanks in advance,


dleal November 11th, 2005 06:31 AM

(I respond in part to my self)

Following the comment from the Expert One-on-One J2EE Design and Development (R. Johnson et al.):

"Unless we are able to use a common base class for all exceptions in an application – something that isn't possible if we mix checked and unchecked exceptions – we will need to make our exceptions implement an ErrorCoded or similarly named interface that defines a method such as this: String getErrorCode();"

..."we are able to distinguish between error messages intended for end users and those intended for developers. Messages inside exceptions (returned by the getMessage() method) should be used for logging, and targeted to developers."

So, the getMessage() is intended for "advanced users/loging" and the getErrorCode() is intended for getting the corresponding real message (language independent) from the properties files defined on the

 <bean id="messageSource"
        class="org.springframework.context.support.Resourc eBundleMessageSource">
        <property name="basename">

so it is more clear for the web tier (on *Controller classes), but how to do with a different tier, lets say in a dos command application that simply invokes the Service Classes (Business Classes) and the errors are catch, how to we get for example access to a simple Junit Test (dos command application) to such error code values.

I gues for testing we should use the method: AbstractDependencyInjectionSpringContextTests.cont extKey() , but I am not sure about that.

Thanks in advance,


All times are GMT -4. The time now is 12:45 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.