This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git

commit 867f37822a3eff7418880eb6000f119c85861836
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sun Apr 7 10:13:12 2024 -0400

    Throw IllegalArgumentException instead of RuntimeException
    
    Throwing RuntimeException is an anti-patten
---
 .../java/org/apache/commons/cli/TypeHandler.java   | 29 ++++++++++++++--------
 1 file changed, 19 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java 
b/src/main/java/org/apache/commons/cli/TypeHandler.java
index a107422..5837ad5 100644
--- a/src/main/java/org/apache/commons/cli/TypeHandler.java
+++ b/src/main/java/org/apache/commons/cli/TypeHandler.java
@@ -76,11 +76,7 @@ public class TypeHandler {
      */
     @Deprecated // since 1.7.0
     public static Date createDate(final String str) {
-        try {
-            return createValue(str, Date.class);
-        } catch (final ParseException e) {
-            throw new RuntimeException(e);
-        }
+        return createValueUnchecked(str, Date.class);
     }
 
     /**
@@ -92,11 +88,7 @@ public class TypeHandler {
      */
     @Deprecated // since 1.7.0
     public static File createFile(final String str) {
-        try {
-            return createValue(str, File.class);
-        } catch (final ParseException e) {
-            throw new RuntimeException(e);
-        }
+        return createValueUnchecked(str, File.class);
     }
 
     /**
@@ -189,6 +181,23 @@ public class TypeHandler {
         return createValue(str, (Class<?>) obj);
     }
 
+    /**
+     * Delegates to {@link #createValue(String, Class)} throwing 
IllegalArgumentException instead of ParseException.
+     *
+     * @param str the command line value
+     * @param clazz the class representing the type of argument
+     * @param <T> type of argument
+     * @return The instance of {@code clazz} initialized with the value of 
{@code str}.
+     * @throws IllegalArgumentException if the value creation for the given 
class threw an exception.
+     */
+    private static <T> T createValueUnchecked(final String str, final Class<T> 
clazz) {
+        try {
+            return createValue(str, clazz);
+        } catch (final ParseException e) {
+            throw new IllegalArgumentException(e);
+        }
+    }
+
     /**
      * Gets the registered converter for the the Class, or {@link 
Converter#DEFAULT} if absent.
      *

Reply via email to