3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-07 06:44:16 +00:00

made Coin.parseCoin method to throw an IllegalArgumentException instead of an ArithmeticException in cases of fractional satoshis

This commit is contained in:
Giannis Dzegoutanis 2014-10-24 11:08:57 +02:00 committed by Mike Hearn
parent e2b00e4cda
commit 683c50b3fa
2 changed files with 9 additions and 2 deletions

View File

@ -128,7 +128,12 @@ public final class Coin implements Monetary, Comparable<Coin>, Serializable {
* @throws IllegalArgumentException if you try to specify fractional satoshis, or a value out of range.
*/
public static Coin parseCoin(final String str) {
return Coin.valueOf(new BigDecimal(str).movePointRight(SMALLEST_UNIT_EXPONENT).toBigIntegerExact().longValue());
try {
long satoshis = new BigDecimal(str).movePointRight(SMALLEST_UNIT_EXPONENT).toBigIntegerExact().longValue();
return Coin.valueOf(satoshis);
} catch (ArithmeticException e) {
throw new IllegalArgumentException(e); // Repackage exception to honor method contract
}
}
public Coin add(final Coin value) {

View File

@ -39,7 +39,9 @@ public class CoinTest {
try {
parseCoin("2E-20");
org.junit.Assert.fail("should not have accepted fractional satoshis");
} catch (ArithmeticException e) {
} catch (IllegalArgumentException expected) {
} catch (Exception e) {
org.junit.Assert.fail("should throw IllegalArgumentException");
}
}