Discussion:
[lang] IllegalArgumentException factory methods
Gary Gregory
2018-10-07 16:13:15 UTC
Permalink
Hi All:

Similar to Objects.requireNonNull() but for IAEs:

/**
* Factory methods for {@link IllegalArgumentException}.
*/
class IllegalArgumentExceptions {

private static <T> T requireNonNull(T obj, String message, Object...
args) {
if (obj == null) {
throw new IllegalArgumentException(String.format(message,
args));
}
return obj;
}

public static <T> T requireNonNull(T obj) {
return requireNonNull(obj, "Argument");
}

public static <T> T requireNonNull(T obj, String argumentName) {
return requireNonNull(obj, "%s MUST not be null", argumentName);
}

}

Suitable for [lang] I would think.

Thoughts?

Gary
Matt Benson
2018-10-07 19:12:46 UTC
Permalink
I would add to Validate with different names, or adopt an entirely new
approach to constructing exceptions, like that demonstrated in [1].

Matt
[1]
https://github.com/apache/bval/blob/bv2/bval-jsr/src/main/java/org/apache/bval/util/Exceptions.java
Post by Gary Gregory
/**
*/
class IllegalArgumentExceptions {
private static <T> T requireNonNull(T obj, String message, Object...
args) {
if (obj == null) {
throw new IllegalArgumentException(String.format(message,
args));
}
return obj;
}
public static <T> T requireNonNull(T obj) {
return requireNonNull(obj, "Argument");
}
public static <T> T requireNonNull(T obj, String argumentName) {
return requireNonNull(obj, "%s MUST not be null", argumentName);
}
}
Suitable for [lang] I would think.
Thoughts?
Gary
Loading...