diff --git a/core/src/main/java/org/bitcoinj/utils/ExchangeRate.java b/core/src/main/java/org/bitcoinj/utils/ExchangeRate.java index 10c928e6..d3b8468d 100644 --- a/core/src/main/java/org/bitcoinj/utils/ExchangeRate.java +++ b/core/src/main/java/org/bitcoinj/utils/ExchangeRate.java @@ -70,6 +70,10 @@ public class ExchangeRate implements Serializable { if (converted.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0 || converted.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) throw new ArithmeticException("Overflow"); - return Coin.valueOf(converted.longValue()); + try { + return Coin.valueOf(converted.longValue()); + } catch (IllegalArgumentException x) { + throw new ArithmeticException("Overflow: " + x.getMessage()); + } } } diff --git a/core/src/test/java/org/bitcoinj/utils/ExchangeRateTest.java b/core/src/test/java/org/bitcoinj/utils/ExchangeRateTest.java index c31a28d1..8c8f2b0f 100644 --- a/core/src/test/java/org/bitcoinj/utils/ExchangeRateTest.java +++ b/core/src/test/java/org/bitcoinj/utils/ExchangeRateTest.java @@ -50,4 +50,28 @@ public class ExchangeRateTest { ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("EUR", "500")); rate.fiatToCoin(Fiat.parseFiat("USD", "1")); } + + @Test(expected = ArithmeticException.class) + public void fiatToCoinTooLarge() throws Exception { + ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1")); + rate.fiatToCoin(Fiat.parseFiat("XXX", "21000001")); + } + + @Test(expected = ArithmeticException.class) + public void fiatToCoinTooSmall() throws Exception { + ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1")); + rate.fiatToCoin(Fiat.parseFiat("XXX", "-21000001")); + } + + @Test(expected = ArithmeticException.class) + public void coinToFiatTooLarge() throws Exception { + ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1000000000")); + rate.coinToFiat(Coin.parseCoin("1000000")); + } + + @Test(expected = ArithmeticException.class) + public void coinToFiatTooSmall() throws Exception { + ExchangeRate rate = new ExchangeRate(Fiat.parseFiat("XXX", "1000000000")); + rate.coinToFiat(Coin.parseCoin("-1000000")); + } }