improved error messages

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/branches/MATH_2_0@712144 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-11-07 14:55:23 +00:00
parent b1592908df
commit 0c7099ab35
22 changed files with 157 additions and 59 deletions

View File

@ -206,7 +206,61 @@ public class MessagesResources_fr
{ "empty selected row index array",
"tableau des indices de lignes s\u00e9lectionn\u00e9es vide" },
{ "empty selected column index array",
"tableau des indices de colonnes s\u00e9lectionn\u00e9es vide" }
"tableau des indices de colonnes s\u00e9lectionn\u00e9es vide" },
// org.apache.commons.math.random.EmpiricalDistributionImpl
// org.apache.commons.math.random.ValueServer
{ "URL {0} contains no data",
"l''adresse {0} ne contient aucune donn\u00e9e" },
// org.apache.commons.math.complex.ComplexFormat
{ "unparseable complex number: \"{0}\"",
"\u00e9chec d''analyse du nombre complexe \"{0}\"" },
// org.apache.commons.math.fraction.FractionFormat
{ "unparseable fraction number: \"{0}\"",
"\u00e9chec d''analyse du nombre rationnel \"{0}\"" },
// org.apache.commons.math.geometry.Vector3DFormat
{ "unparseable 3D vector: \"{0}\"",
"\u00e9chec d''analyse du vecteur de dimension 3 \"{0}\"" },
// org.apache.commons.math.linear.RealVectorFormat
{ "unparseable real vector: \"{0}\"",
"\u00e9chec d''analyse du vecteur r\u00e9el \"{0}\"" },
// org.apache.commons.math.util.ResizableDoubleArray
{ "the index specified: {0} is larger than the current maximal index {1}",
"l''index sp\u00e9cifi\u00e9 ({0}) d\u00e9passe l''index maximal courant ({1})" },
{ "elements cannot be retrieved from a negative array index {0}",
"impossible d''extraire un \u00e9l\u00e9ment \u00e0 un index n\u00e9gatif ({0})" },
{ "cannot set an element at a negative index {0}",
"impossible de mettre un \u00e9l\u00e9ment \u00e0 un index n\u00e9gatif ({0})" },
// org.apache.commons.math.analysis.PolynomialFunctionLagrangeForm
{ "identical abscissas x[{0}] == x[{1}] == {2} cause division by zero",
"division par z\u00e9ro caus\u00e9e par les abscisses identiques x[{0}] == x[{1}] == {2}" },
// org.apache.commons.math.fraction.Fraction
{ "zero denominator in fraction {0}/{1}",
"d\u00e9nominateur null dans le nombre rationnel {0}/{1}" },
{ "overflow in fraction {0}/{1}, cannot negate",
"d\u00e9passement de capacit\u00e9 pour la fraction {0}/{1}, son signe ne peut \u00eatre chang\u00e9" },
{ "overflow, numerator too large after multiply: {0}",
"d\u00e9passement de capacit\u00e9 pour le num\u00e9rateur apr\u00e8s multiplication : {0}" },
{ "the fraction to divide by must not be zero: {0}/{1}",
"division par un nombre rationnel nul : {0}/{1}" },
// org.apache.commons.math.geometry.Rotation
{ "zero norm for rotation axis",
"norme nulle pour un axe de rotation" },
// org.apache.commons.math.geometry.Vector3D
// org.apache.commons.math.linear.RealVectorImpl
{ "cannot normalize a zero norm vector",
"impossible de normer un vecteur de norme nulle" },
{ "zero norm",
"norme nulle" }
};

View File

@ -20,6 +20,7 @@ import java.io.Serializable;
import org.apache.commons.math.DuplicateSampleAbscissaException;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.MathRuntimeException;
/**
* Implements the representation of a real polynomial function in
@ -253,8 +254,14 @@ public class PolynomialFunctionLagrangeForm implements UnivariateRealFunction,
}
if (d == 0.0) {
// This happens only when two abscissas are identical.
throw new ArithmeticException
("Identical abscissas cause division by zero.");
for (int k = 0; k < n; ++k) {
if ((i != k) && (x[i] == x[k])) {
throw MathRuntimeException.createArithmeticException("identical abscissas x[{0}] == x[{1}] == {2} cause division by zero",
new Object[] {
i, k, x[i]
});
}
}
}
t = y[i] / d;
// Lagrange polynomial is the sum of n terms, each of which is a

View File

@ -23,6 +23,7 @@ import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.CompositeFormat;
/**
@ -250,8 +251,9 @@ public class ComplexFormat extends CompositeFormat {
ParsePosition parsePosition = new ParsePosition(0);
Complex result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable complex number: \"" + source +
"\"", parsePosition.getErrorIndex());
throw MathRuntimeException.createParseException("unparseable complex number: \"{0}\"",
new Object[] { source },
parsePosition.getErrorIndex());
}
return result;
}

View File

@ -182,7 +182,7 @@ public abstract class AbstractEstimator implements Estimator {
return ((RealMatrixImpl) new RealMatrixImpl(jTj, false).inverse()).getDataRef();
} catch (InvalidMatrixException ime) {
throw new EstimationException("unable to compute covariances: singular problem",
new Object[0]);
null);
}
}

View File

@ -161,7 +161,7 @@ public class GaussNewtonEstimator extends AbstractEstimator implements Serializa
}
} catch(InvalidMatrixException e) {
throw new EstimationException("unable to solve: singular problem", new Object[0]);
throw new EstimationException("unable to solve: singular problem", null);
}

View File

@ -17,6 +17,8 @@
package org.apache.commons.math.fraction;
import java.math.BigInteger;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils;
/**
@ -200,12 +202,13 @@ public class Fraction extends Number implements Comparable<Fraction> {
public Fraction(int num, int den) {
super();
if (den == 0) {
throw new ArithmeticException("The denominator must not be zero");
throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
new Object[] { num, den});
}
if (den < 0) {
if (num == Integer.MIN_VALUE ||
den == Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: can't negate");
if (num == Integer.MIN_VALUE || den == Integer.MIN_VALUE) {
throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
new Object[] { num, den});
}
num = -num;
den = -den;
@ -361,7 +364,8 @@ public class Fraction extends Number implements Comparable<Fraction> {
*/
public Fraction negate() {
if (numerator==Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: too large to negate");
throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
new Object[] { numerator, denominator});
}
return new Fraction(-numerator, denominator);
}
@ -451,8 +455,8 @@ public class Fraction extends Number implements Comparable<Fraction> {
// result is (t/d2) / (u'/d1)(v'/d2)
BigInteger w = t.divide(BigInteger.valueOf(d2));
if (w.bitLength() > 31) {
throw new ArithmeticException
("overflow: numerator too large after multiply");
throw MathRuntimeException.createArithmeticException("overflow, numerator too large after multiply: {0}",
new Object[] { w });
}
return new Fraction (w.intValue(),
MathUtils.mulAndCheck(denominator/d1,
@ -500,7 +504,8 @@ public class Fraction extends Number implements Comparable<Fraction> {
throw new IllegalArgumentException("The fraction must not be null");
}
if (fraction.numerator == 0) {
throw new ArithmeticException("The fraction to divide by must not be zero");
throw MathRuntimeException.createArithmeticException("the fraction to divide by must not be zero: {0}/{1}",
new Object[] { fraction.numerator, fraction.denominator });
}
return multiply(fraction.reciprocal());
}
@ -518,7 +523,8 @@ public class Fraction extends Number implements Comparable<Fraction> {
*/
public static Fraction getReducedFraction(int numerator, int denominator) {
if (denominator == 0) {
throw new ArithmeticException("The denominator must not be zero");
throw MathRuntimeException.createArithmeticException("zero denominator in fraction {0}/{1}",
new Object[] { numerator, denominator});
}
if (numerator==0) {
return ZERO; // normalize zero.
@ -530,7 +536,8 @@ public class Fraction extends Number implements Comparable<Fraction> {
if (denominator < 0) {
if (numerator==Integer.MIN_VALUE ||
denominator==Integer.MIN_VALUE) {
throw new ArithmeticException("overflow: can't negate");
throw MathRuntimeException.createArithmeticException("overflow in fraction {0}/{1}, cannot negate",
new Object[] { numerator, denominator});
}
numerator = -numerator;
denominator = -denominator;

View File

@ -26,6 +26,7 @@ import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math.ConvergenceException;
import org.apache.commons.math.MathRuntimeException;
/**
* Formats a Fraction number in proper format or improper format. The number
@ -244,8 +245,9 @@ public class FractionFormat extends Format implements Serializable {
ParsePosition parsePosition = new ParsePosition(0);
Fraction result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable fraction number: \"" +
source + "\"", parsePosition.getErrorIndex());
throw MathRuntimeException.createParseException("unparseable fraction number: \"{0}\"",
new Object[] { source },
parsePosition.getErrorIndex());
}
return result;
}

View File

@ -19,6 +19,8 @@ package org.apache.commons.math.geometry;
import java.io.Serializable;
import org.apache.commons.math.MathRuntimeException;
/**
* This class implements rotations in a three-dimensional space.
*
@ -154,7 +156,8 @@ public class Rotation implements Serializable {
double norm = axis.getNorm();
if (norm == 0) {
throw new ArithmeticException("zero norm for rotation axis");
throw MathRuntimeException.createArithmeticException("zero norm for rotation axis",
null);
}
double halfAngle = -0.5 * angle;

View File

@ -19,6 +19,7 @@ package org.apache.commons.math.geometry;
import java.io.Serializable;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils;
/**
@ -266,7 +267,8 @@ public class Vector3D
public Vector3D normalize() {
double s = getNorm();
if (s == 0) {
throw new ArithmeticException("cannot normalize a zero norm vector");
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector",
null);
}
return scalarMultiply(1 / s);
}
@ -290,7 +292,7 @@ public class Vector3D
double threshold = 0.6 * getNorm();
if (threshold == 0) {
throw new ArithmeticException("null norm");
throw MathRuntimeException.createArithmeticException("zero norm", null);
}
if ((x >= -threshold) && (x <= threshold)) {
@ -320,7 +322,7 @@ public class Vector3D
double normProduct = v1.getNorm() * v2.getNorm();
if (normProduct == 0) {
throw new ArithmeticException("null norm");
throw MathRuntimeException.createArithmeticException("zero norm", null);
}
double dot = dotProduct(v1, v2);

View File

@ -23,6 +23,7 @@ import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Locale;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.CompositeFormat;
/**
@ -254,8 +255,9 @@ public class Vector3DFormat extends CompositeFormat {
ParsePosition parsePosition = new ParsePosition(0);
Vector3D result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable 3D vector: \"" + source +
"\"", parsePosition.getErrorIndex());
throw MathRuntimeException.createParseException("unparseable 3D vector: \"{0}\"",
new Object[] { source },
parsePosition.getErrorIndex());
}
return result;
}

View File

@ -622,9 +622,9 @@ public class BigMatrixImpl implements BigMatrix, Serializable {
if (selectedRows.length * selectedColumns.length == 0) {
if (selectedRows.length == 0) {
throw new MatrixIndexException("empty selected row index array", new Object[0]);
throw new MatrixIndexException("empty selected row index array", null);
}
throw new MatrixIndexException("empty selected column index array", new Object[0]);
throw new MatrixIndexException("empty selected column index array", null);
}
final BigDecimal[][] subMatrixData =

View File

@ -413,9 +413,9 @@ public class RealMatrixImpl implements RealMatrix, Serializable {
if (selectedRows.length * selectedColumns.length == 0) {
if (selectedRows.length == 0) {
throw new MatrixIndexException("empty selected row index array", new Object[0]);
throw new MatrixIndexException("empty selected row index array", null);
}
throw new MatrixIndexException("empty selected column index array", new Object[0]);
throw new MatrixIndexException("empty selected column index array", null);
}
final double[][] subMatrixData =

View File

@ -25,6 +25,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.CompositeFormat;
/**
@ -258,8 +259,9 @@ public class RealVectorFormat extends CompositeFormat {
ParsePosition parsePosition = new ParsePosition(0);
RealVectorImpl result = parse(source, parsePosition);
if (parsePosition.getIndex() == 0) {
throw new ParseException("Unparseable real vector: \"" + source +
"\"", parsePosition.getErrorIndex());
throw MathRuntimeException.createParseException("unparseable real vector: \"{0}\"",
new Object[] { source },
parsePosition.getErrorIndex());
}
return result;
}

View File

@ -19,6 +19,7 @@ package org.apache.commons.math.linear;
import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.util.MathUtils;
/**
@ -1068,7 +1069,7 @@ public class RealVectorImpl implements RealVector, Serializable {
public RealVector unitVector() throws ArithmeticException {
final double norm = getNorm();
if (norm == 0) {
throw new ArithmeticException("null norm");
throw MathRuntimeException.createArithmeticException("zero norm", null);
}
return mapDivide(getNorm());
}
@ -1077,7 +1078,8 @@ public class RealVectorImpl implements RealVector, Serializable {
public void unitize() throws ArithmeticException {
final double norm = getNorm();
if (norm == 0) {
throw new ArithmeticException("null norm");
throw MathRuntimeException.createArithmeticException("cannot normalize a zero norm vector",
null);
}
for (int i = 0; i < data.length; i++) {
data[i] /= norm;

View File

@ -315,7 +315,7 @@ public class ContinuousOutputModel
steps.get(index).setInterpolatedTime(time);
} catch (DerivativeException de) {
throw new MathRuntimeException("unexpected exception caught", new Object[0], de);
throw new MathRuntimeException("unexpected exception caught", null, de);
}
}

View File

@ -270,7 +270,7 @@ public abstract class DirectSearchOptimizer {
} catch (DimensionMismatchException dme) {
// this should not happen
throw new MathRuntimeException("unexpected exception caught", new Object[0], dme);
throw new MathRuntimeException("unexpected exception caught", null, dme);
}
}

View File

@ -18,7 +18,6 @@
package org.apache.commons.math.random;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
@ -130,11 +129,18 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
DataAdapter da = new StreamDataAdapter(in);
try {
da.computeStats();
} catch (IOException ioe) {
// don't wrap exceptions which are already IOException
throw ioe;
} catch (RuntimeException rte) {
// don't wrap RuntimeExceptions
throw rte;
} catch (Exception e) {
throw new IOException(e.getMessage());
}
if (sampleStats.getN() == 0) {
throw new EOFException("URL " + url + " contains no data");
throw MathRuntimeException.createEOFException("URL {0} contains no data",
new Object[] { url });
}
in = new BufferedReader(new InputStreamReader(url.openStream()));
fillBinStats(in);
@ -162,6 +168,12 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
DataAdapter da = new StreamDataAdapter(in);
try {
da.computeStats();
} catch (IOException ioe) {
// don't wrap exceptions which are already IOException
throw ioe;
} catch (RuntimeException rte) {
// don't wrap RuntimeExceptions
throw rte;
} catch (Exception e) {
throw new IOException(e.getMessage());
}
@ -357,12 +369,14 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
DataAdapter da = aFactory.getAdapter(in);
try {
da.computeBinStats(min, delta);
} catch (IOException ioe) {
// don't wrap exceptions which are already IOException
throw ioe;
} catch (RuntimeException rte) {
// don't wrap RuntimeExceptions
throw rte;
} catch (Exception e) {
if(e instanceof RuntimeException){
throw new RuntimeException(e.getMessage());
}else{
throw new IOException(e.getMessage());
}
throw new IOException(e.getMessage());
}
// Assign upperBounds based on bin counts
@ -419,7 +433,7 @@ public class EmpiricalDistributionImpl implements Serializable, EmpiricalDistrib
}
}
}
throw new MathRuntimeException("no bin selected", new Object[0]);
throw new MathRuntimeException("no bin selected", null);
}
/**

View File

@ -17,11 +17,12 @@
package org.apache.commons.math.random;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.math.MathRuntimeException;
/**
* Generates values for use in simulation applications.
@ -328,7 +329,8 @@ public class ValueServer {
closeReplayFile();
resetReplayFile();
if ((str = filePointer.readLine()) == null) {
throw new EOFException("URL " + valuesFileURL + " contains no data");
throw MathRuntimeException.createEOFException("URL {0} contains no data",
new Object[] { valuesFileURL });
}
}
return Double.valueOf(str).doubleValue();

View File

@ -44,7 +44,7 @@ public class DefaultTransformer implements NumberTransformer, Serializable {
public double transform(Object o) throws MathException{
if (o == null) {
throw new MathException("Conversion Exception in Transformation, Object is null", new Object[0]);
throw new MathException("Conversion Exception in Transformation, Object is null", null);
}
if (o instanceof Number) {

View File

@ -18,6 +18,8 @@ package org.apache.commons.math.util;
import java.io.Serializable;
import org.apache.commons.math.MathRuntimeException;
/**
* <p>
* A variable length {@link DoubleArray} implementation that automatically
@ -454,16 +456,13 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
*/
public synchronized double getElement(int index) {
if (index >= numElements) {
String msg =
"The index specified: " + index +
" is larger than the current number of elements";
throw new ArrayIndexOutOfBoundsException(msg);
throw MathRuntimeException.createArrayIndexOutOfBoundsException("the index specified: {0} is larger than the current maximal index {1}",
new Object[] { index, numElements - 1 });
} else if (index >= 0) {
return internalArray[startIndex + index];
} else {
String msg =
"Elements cannot be retrieved from a negative array index";
throw new ArrayIndexOutOfBoundsException(msg);
throw MathRuntimeException.createArrayIndexOutOfBoundsException("elements cannot be retrieved from a negative array index {0}",
new Object[] { index });
}
}
@ -570,8 +569,8 @@ public class ResizableDoubleArray implements DoubleArray, Serializable {
*/
public synchronized void setElement(int index, double value) {
if (index < 0) {
String msg = "Cannot set an element at a negative index";
throw new ArrayIndexOutOfBoundsException(msg);
throw MathRuntimeException.createArrayIndexOutOfBoundsException("cannot set an element at a negative index {0}",
new Object[] { index });
}
if (index + 1 > numElements) {
numElements = index + 1;

View File

@ -783,7 +783,7 @@ public final class BigMatrixImplTest extends TestCase {
if (!lu.isSquare() || lowerData.length != lowerData[0].length || upperData.length != upperData[0].length ||
lowerData.length != upperData.length
|| lowerData.length != lu.getRowDimension()) {
throw new InvalidMatrixException("incorrect dimensions", new Object[0]);
throw new InvalidMatrixException("incorrect dimensions", null);
}
int n = lu.getRowDimension();
for (int i = 0; i < n; i++) {

View File

@ -59,7 +59,7 @@ public class TestBean {
*
*/
public Double getZ() {
throw new MathRuntimeException();
throw new MathRuntimeException("?", null);
}
/**