Skip to content

Commit 867f378

Browse files
committed
Throw IllegalArgumentException instead of RuntimeException
Throwing RuntimeException is an anti-patten
1 parent a706635 commit 867f378

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public static Class<?> createClass(final String className) throws ParseException
7676
*/
7777
@Deprecated // since 1.7.0
7878
public static Date createDate(final String str) {
79-
try {
80-
return createValue(str, Date.class);
81-
} catch (final ParseException e) {
82-
throw new RuntimeException(e);
83-
}
79+
return createValueUnchecked(str, Date.class);
8480
}
8581

8682
/**
@@ -92,11 +88,7 @@ public static Date createDate(final String str) {
9288
*/
9389
@Deprecated // since 1.7.0
9490
public static File createFile(final String str) {
95-
try {
96-
return createValue(str, File.class);
97-
} catch (final ParseException e) {
98-
throw new RuntimeException(e);
99-
}
91+
return createValueUnchecked(str, File.class);
10092
}
10193

10294
/**
@@ -189,6 +181,23 @@ public static Object createValue(final String str, final Object obj) throws Pars
189181
return createValue(str, (Class<?>) obj);
190182
}
191183

184+
/**
185+
* Delegates to {@link #createValue(String, Class)} throwing IllegalArgumentException instead of ParseException.
186+
*
187+
* @param str the command line value
188+
* @param clazz the class representing the type of argument
189+
* @param <T> type of argument
190+
* @return The instance of {@code clazz} initialized with the value of {@code str}.
191+
* @throws IllegalArgumentException if the value creation for the given class threw an exception.
192+
*/
193+
private static <T> T createValueUnchecked(final String str, final Class<T> clazz) {
194+
try {
195+
return createValue(str, clazz);
196+
} catch (final ParseException e) {
197+
throw new IllegalArgumentException(e);
198+
}
199+
}
200+
192201
/**
193202
* Gets the registered converter for the the Class, or {@link Converter#DEFAULT} if absent.
194203
*

0 commit comments

Comments
 (0)