mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-24 22:58:32 +01: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
2 changed files with 9 additions and 2 deletions
|
@ -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) {
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue