diff --git a/core/src/main/java/org/bitcoinj/core/Coin.java b/core/src/main/java/org/bitcoinj/core/Coin.java index 4d8728a94..8a81c5356 100644 --- a/core/src/main/java/org/bitcoinj/core/Coin.java +++ b/core/src/main/java/org/bitcoinj/core/Coin.java @@ -128,7 +128,12 @@ public final class Coin implements Monetary, Comparable, 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) { diff --git a/core/src/test/java/org/bitcoinj/core/CoinTest.java b/core/src/test/java/org/bitcoinj/core/CoinTest.java index 362a8df0d..2ac1b150c 100644 --- a/core/src/test/java/org/bitcoinj/core/CoinTest.java +++ b/core/src/test/java/org/bitcoinj/core/CoinTest.java @@ -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"); } }