My specific problem is how to handle internationalization of specific messages from an architecture layer that is not the view layer(e.g service layer).

I've created an architecture based on technology to be used (e.g. jsf, primefaces, spring, postgres and mybatis). So to avoid rewriting code i've defined a project to be a jar (with maven) that has all generic (abstract) classes that one should implement (based on interfaces). Something like (to keep it short I will focus in one functionality and I will not add the dao classes)

 |_ service
 |   |_ GenericService.java
 |   |_ AbstractGenericServiceImpl.java
 |   |_ GenericServiceException.java
 |_ domain
 |   |_ GenericDomain.java
 |_ view
     |_ AbstractGenericManagedBean.java

With this in a jar file I can create an application that will abstract all the repetitive operations like add, edit, delete, list, etc.

In my AbstractGenericServiceImpl all operations throws GenericServiceException with a message based on the original exception like this:

public void add(E entity) throws D {
    try {
    } catch (DuplicateKeyException ex) {
        throw getNewException("Duplicate registry. Can not add.");

This is just one of many exceptions that I catch and throw as my GenericServiceException.

So in my web application I have all necessary structure to handle internationalization as the JSF 2.x specification recommend the i18n package with the properties files whithin e.g. (messages.properties, messages_en.properties, messages_pt_BR.properties and so on.) and it is configured on the faces-config.xml it all works fine FOR MY XHTML files but what about my service files which is decoupled of my web app? How can I change my messages from the getNewException("Duplicate registry. Can not add.") to something like


Is this a good approach or I should let the generic service always throw every exception to be handled by the managed beans that called the service?

I'm asking this because every question that I find about it only speak on the view layer.

Code for the method getNewException:

private D getNewException(String message) {
    try {
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        Class<D> theType = (Class<D>) type.getActualTypeArguments()[2];
        Constructor<D> constructor = theType.getConstructor(String.class);
        return constructor.newInstance(message);
    } catch (NoSuchMethodException | SecurityException 
                 | InstantiationException 
                 | IllegalAccessException 
                 | IllegalArgumentException
                 | InvocationTargetException e) {
        logger.error("Erro ao executar o reflection para exceção. " + e.getMessage());
    return null;

1 Answer 1


The type of the exception that is thrown should not be specific to the human language domain.

Throw some exception that's named in English, and look up an internationalized string for the exception description.

  • That method getNewException returns an instance of the exception choosen by the programmer of the web application that should extends my own GenericServiceException so in a class created by him they can throw any exception as he wants as long as he follow the contract. What you say is that I should create an specific exception to each one that I catch and then throw it forward so the application (view) can handle the message internationalized. This is the definition of my genericservice interface: public interface GenericService<E extends BaseDomain, T extends GenericServiceException> Apr 15, 2015 at 22:28
  • Well, I'm still trying to figure out why you need a Factory Method to get your exception in the first place. The whole point of a factory method is to vary the return type based on a string setting or some similar condition. Internationalization would qualify as such a setting. What I'm saying is to throw a DuplicateRegistryException, and either lookup an internationalized description string when you throw it, or simply throw it without a description. Using a factory method to do this seems like overkill. Apr 15, 2015 at 22:30
  • It is a method to create an instance of the parameterized exception if it is created on the application. My methods on the interface is like: void add(E entity) throws T; so in the implementation I can throw forward the exception on the webapp something like WebAppException extends GenericServiceException and my abstract service would know what instance of the exception it should throw. Am I wrong with this approach ? +1 for your answer and help anyway! Apr 15, 2015 at 22:36
  • Why do you need this additional layer of abstraction? What does it add that merely throwing WebAppException directly doesn't already provide? throw new WebAppException(getInternationalizedExceptionString(blah)) Apr 15, 2015 at 22:38
  • This would work WebAppException(getInternationalizedExceptionString(blah)) for the exceptions on the web application my problem is with the ones on the abstraction Apr 15, 2015 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.