ICU-13808 Document ArithmeticException thrown by DecimalFormat

- Document the exception in the following methods
setMultiplier, setMathContext, setMathContextICU
- Add test to check the documented behavior
This commit is contained in:
Victor Chang 2018-08-24 15:53:15 +01:00 committed by Shane Carr
parent dbd9065c69
commit ac0c7bbe3c
No known key found for this signature in database
GPG Key ID: FCED3B24AAB18B5C
2 changed files with 50 additions and 3 deletions

View File

@ -1123,6 +1123,8 @@ public class DecimalFormat extends NumberFormat {
*
* @param multiplier The number by which all numbers passed to {@link #format} will be multiplied.
* @throws IllegalArgumentException If the given multiplier is zero.
* @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
* in conjunction with MathContext of unlimited precision.
* @category Multipliers
* @stable ICU 2.0
*/
@ -1296,6 +1298,8 @@ public class DecimalFormat extends NumberFormat {
* method.
*
* @param mathContext The MathContext to use when rounding numbers.
* @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
* in conjunction with MathContext of unlimited precision.
* @see java.math.MathContext
* @category Rounding
* @stable ICU 4.2
@ -1330,6 +1334,8 @@ public class DecimalFormat extends NumberFormat {
* {@link com.ibm.icu.math.MathContext}.
*
* @param mathContextICU The MathContext to use when rounding numbers.
* @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result
* in conjunction with MathContext of unlimited precision.
* @see #setMathContext(java.math.MathContext)
* @category Rounding
* @stable ICU 4.2

View File

@ -4962,10 +4962,51 @@ public class NumberFormatTest extends TestFmwk {
df.setMathContext(fourDigits);
BigInteger actual4Digits = ((BigDecimal) df.parse(hugeNumberString)).toBigIntegerExact();
assertEquals("Extreme division with fourDigits", huge4Digits, actual4Digits);
}
/**
* ArithmeticException is thrown when inverting multiplier produces a non-terminating
* decimal result in conjunction with MathContext of unlimited precision.
*/
@Test
public void testSetMathContextArithmeticException() {
DecimalFormat df = new DecimalFormat();
df.setMultiplier(7);
try {
df.setMathContext(unlimitedCeiling);
df.parse(hugeNumberString);
fail("Extreme division with unlimitedCeiling should throw ArithmeticException");
df.setMathContext(java.math.MathContext.UNLIMITED);
fail("Extreme division with unlimited precision should throw ArithmeticException");
} catch (ArithmeticException e) {
// expected
}
}
/**
* ArithmeticException is thrown when inverting multiplier produces a non-terminating
* decimal result in conjunction with MathContext of unlimited precision.
*/
@Test
public void testSetMathContextICUArithmeticException() {
DecimalFormat df = new DecimalFormat();
df.setMultiplier(7);
try {
df.setMathContextICU(new MathContext(0));
fail("Extreme division with unlimited precision should throw ArithmeticException");
} catch (ArithmeticException e) {
// expected
}
}
/**
* ArithmeticException is thrown when inverting multiplier produces a non-terminating
* decimal result in conjunction with MathContext of unlimited precision.
*/
@Test
public void testSetMultiplierArithmeticException() {
DecimalFormat df = new DecimalFormat();
df.setMathContext(java.math.MathContext.UNLIMITED);
try {
df.setMultiplier(7);
fail("Extreme division with unlimited precision should throw ArithmeticException");
} catch (ArithmeticException e) {
// expected
}