Localized exceptions in java
Asked Answered
E

2

6

I have an interface for validation of user's input. To validate I use some standard Java validators and some custom ones. My custom validators throws a ParseException with a localized message on error. But I don't know whether Java or third party lib also uses localized messages.

Question: how I get a hint in catch-block whether the exception has a localized message or not (a standard Java way, not a custom exception class/interface)?

Elbow answered 8/10, 2013 at 10:58 Comment(0)
O
4

You can simply use getLocalizedMessage() to display the message, the default implementation just calls getMessage().

From the documentation of the Throwable class:

public String getLocalizedMessage()
Creates a localized description of this throwable. Subclasses may override this method in order to produce a locale-specific message. For subclasses that do not override this method, the default implementation returns the same result as getMessage().

Ossuary answered 8/10, 2013 at 11:0 Comment(3)
It's not what I search. I only whant to know whether the exception contains something, which can be shown to the user (in his language) or not.Elbow
@SergiyMedvynskyy I'm afraid that is generally not possible... For example, any code can throw new NumberFormatException("Nicht gut") from a validation code - but it is not possible to tell which language it was. Also, on a different thought: I would not let the user see any exceptions. Use "pokemon" tactic: Catch them all, and handle them appropriately, and if there is something to be reported to the user, do it in a customised way.Ossuary
It was also my approach, but I thought, probably exists a better way.Elbow
I
1

This is a simple demo.

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Optional;
import java.util.ResourceBundle; 
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.inject.spi.CDI;
import javax.servlet.http.HttpServletRequest;

public abstract class AbstractException extends RuntimeException {

    // get locale from request if you are using cdi
    @Context
    private HttpServletRequest request;

    @Override
    public String getLocalizedMessage() {                
        BeanManager beanManager = CDI.current().getBeanManager();
        HttpServletRequest request = HttpServletRequest.class
            .cast(beanManager.getReference(beanManager.resolve(beanManager.getBeans(HttpServletRequest.class)),
                    HttpServletRequest.class, beanManager.createCreationalContext(null)));
    
        Locale locale = Optional.ofNullable(request).map(HttpServletRequest::getLocale)
                .orElseGet(Locale::getDefault); 
        String message;
        try {
            message = ResourceBundle.getBundle("messages", locale).getString(getClass().getName());
        } catch (MissingResourceException e) {
            message = super.getLocalizedMessage();
        }
        return message;
    }
}
public class ExceptionImpl extends AbstractException {
    public static void main(String[] args) {
        throw new ExceptionImpl();
    }
}

The content of messages_en_US.properties:

ExceptionImpl=this is message!
Indebted answered 8/10, 2023 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.