From eecbca6894ba731524a7fb91becce11de4071b2a Mon Sep 17 00:00:00 2001 From: emopers Date: Mon, 7 Dec 2015 23:46:04 -0600 Subject: [PATCH] Fixing uncaught exception --- src/main/java/com/greplin/interval/LongInterval.java | 10 +++++++--- .../java/com/greplin/interval/LongIntervalTest.java | 5 +++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/greplin/interval/LongInterval.java b/src/main/java/com/greplin/interval/LongInterval.java index 63dc3bd..890ceb6 100644 --- a/src/main/java/com/greplin/interval/LongInterval.java +++ b/src/main/java/com/greplin/interval/LongInterval.java @@ -105,9 +105,13 @@ public boolean intersects(final LongInterval other) { public static LongInterval valueOf(final String value) { String trimmed = value.trim(); int middle = trimmed.indexOf('-', 1); - long start = Long.parseLong(trimmed.substring(0, middle).trim()); - long end = Long.parseLong(trimmed.substring(middle + 1).trim()); - return new LongInterval(start, end); + try { + long start = Long.parseLong(trimmed.substring(0, middle).trim()); + long end = Long.parseLong(trimmed.substring(middle + 1).trim()); + return new LongInterval(start, end); + } catch(NumberFormatException e) { + throw new NumberFormatException("Passed value does not contain two parsable long values"); + } } diff --git a/src/test/java/com/greplin/interval/LongIntervalTest.java b/src/test/java/com/greplin/interval/LongIntervalTest.java index 5475043..9a629e8 100644 --- a/src/test/java/com/greplin/interval/LongIntervalTest.java +++ b/src/test/java/com/greplin/interval/LongIntervalTest.java @@ -21,6 +21,11 @@ public void testNegativeNumbers() throws Exception { assertParse("-2--1", -2, -1); } + @Test(expected=NumberFormatException.class) + public void testEmptyNumbers() throws Exception { + LongInterval result = LongInterval.valueOf("null-null"); + } + @Test public void testEquals() throws Exception { Assert.assertEquals(new LongInterval(1, 100), new LongInterval(1, 100));