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:
parent
e2b00e4cda
commit
683c50b3fa
@ -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.
|
* @throws IllegalArgumentException if you try to specify fractional satoshis, or a value out of range.
|
||||||
*/
|
*/
|
||||||
public static Coin parseCoin(final String str) {
|
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) {
|
public Coin add(final Coin value) {
|
||||||
|
@ -39,7 +39,9 @@ public class CoinTest {
|
|||||||
try {
|
try {
|
||||||
parseCoin("2E-20");
|
parseCoin("2E-20");
|
||||||
org.junit.Assert.fail("should not have accepted fractional satoshis");
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user