diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java index 8e5d45bd2..43f8ed34a 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalArgumentException.java @@ -76,7 +76,6 @@ public class MathIllegalArgumentException extends IllegalArgumentException { * Get the message in a specified locale. * * @param locale Locale in which the message should be translated. - * * @return the localized message. */ public String getMessage(final Locale locale) { diff --git a/src/main/java/org/apache/commons/math/exception/MathUserException.java b/src/main/java/org/apache/commons/math/exception/MathUserException.java index 44e7c8066..4566d515e 100644 --- a/src/main/java/org/apache/commons/math/exception/MathUserException.java +++ b/src/main/java/org/apache/commons/math/exception/MathUserException.java @@ -16,6 +16,13 @@ */ package org.apache.commons.math.exception; +import java.util.Locale; + +import org.apache.commons.math.exception.util.ArgUtils; +import org.apache.commons.math.exception.util.MessageFactory; +import org.apache.commons.math.exception.util.Localizable; +import org.apache.commons.math.exception.util.LocalizedFormats; + /** * This class is intended as a sort of communication channel between * layers of user code separated from each other by calls to @@ -26,31 +33,67 @@ package org.apache.commons.math.exception; * @version $Revision$ $Date$ */ public class MathUserException extends RuntimeException { + /** Serializable version Id. */ + private static final long serialVersionUID = -6024911025449780478L; + /** + * Pattern used to build the message (problem description). + */ + private final Localizable pattern; + /** + * Arguments used to build the message. + */ + private final Object[] arguments; + /** * Default constructor. */ - public MathUserException() {} - - /** - * @param msg Error message. - */ - public MathUserException(String msg) { - super(msg); - } - - /** - * @param msg Error message. - * @param cause Cause of the error. - */ - public MathUserException(String msg, - Throwable cause) { - super(msg, cause); + public MathUserException() { + this(null); } /** * @param cause Cause of the error. + * @param args Arguments. */ - public MathUserException(Throwable cause) { - super(cause); + public MathUserException(Throwable cause, + Object ... args) { + this(null, cause, args); + } + + /** + * @param pattern Message pattern explaining the cause of the error. + * @param cause Cause of the error. + * @param args Arguments. + */ + public MathUserException(Localizable pattern, + Throwable cause, + Object ... args) { + this.pattern = pattern; + arguments = ArgUtils.flatten(args); + } + + /** + * Get the message in a specified locale. + * + * @param locale Locale in which the message should be translated. + * @return the localized message. + */ + public String getMessage(final Locale locale) { + return MessageFactory.buildMessage(locale, + pattern, + LocalizedFormats.USER_EXCEPTION, + arguments); + } + + /** {@inheritDoc} */ + @Override + public String getMessage() { + return getMessage(Locale.US); + } + + /** {@inheritDoc} */ + @Override + public String getLocalizedMessage() { + return getMessage(Locale.getDefault()); } } diff --git a/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java b/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java index 38cd766c0..8cb9ab1cc 100644 --- a/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/exception/util/LocalizedFormats.java @@ -303,6 +303,7 @@ public enum LocalizedFormats implements Localizable { UNPARSEABLE_REAL_VECTOR("unparseable real vector: \"{0}\""), UNSUPPORTED_EXPANSION_MODE("unsupported expansion mode {0}, supported modes are {1} ({2}) and {3} ({4})"), UNSUPPORTED_OPERATION("unsupported operation"), /* keep */ + USER_EXCEPTION("exception generated in user code"), /* keep */ URL_CONTAINS_NO_DATA("URL {0} contains no data"), VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC("{0} values have been added before statistic is configured"), VECTOR_LENGTH_MISMATCH("vector length mismatch: got {0} but expected {1}"), diff --git a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties index c5be758c4..e2b9d12d9 100644 --- a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties +++ b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties @@ -275,6 +275,7 @@ UNPARSEABLE_FRACTION_NUMBER = nombre fractionnaire non analysable : "{0}" UNPARSEABLE_REAL_VECTOR = vecteur r\u00e9el non analysable : "{0}" UNSUPPORTED_EXPANSION_MODE = mode d''extension {0} non support\u00e9, les modes support\u00e9s sont {1} ({2}) et {3} ({4}) UNSUPPORTED_OPERATION = op\u00e9ration non disponible +USER_EXCEPTION = erreur g\u00e9n\u00e9r\u00e9e par le code utilisateur URL_CONTAINS_NO_DATA = l''adresse {0} ne contient aucune donn\u00e9e VALUES_ADDED_BEFORE_CONFIGURING_STATISTIC = {0} valeurs ont \u00e9t\u00e9 ajout\u00e9es VECTOR_LENGTH_MISMATCH = taille de vecteur invalide : {0} au lieu de {1} attendue