Validate a string for positive number and normal string I have a method in which I am accepting a String clientid and that has below requirements:


clientid can be a positive number greater than zero. But if it is negative number or zero, then throw IllegalArgumentException with a message.
clientid cannot be a null or empty string. But if it is, then throw IllegalArgumentExceptionwith a message.
clientid can be a normal string as well. For example - it can be abcdefgh or any other string.




import static com.google.common.base.Preconditions.checkArgument;

public Builder setClientId(String clientid) {
    checkArgument(!Strings.isNullOrEmpty(clientid), "clientid cannot not be null or an empty string, found '%s'.",
            clientid);
    try {
        final long id = Long.parseLong(clientid);
        checkArgument(id &gt; 0, "clientid must not be negative or zero, found '%s'.", clientid);
    } catch (NumberFormatException e) {}
    this.clientid = clientid;
    return this;
}


I cannot use Guava library greater than 11 so I cannot use Longs.tryParse method here so just using normal ugly way of parsing string. Is there anything I can improve here which is without try catch block? Anything from Apache Commons?
If all negative and zero number strings are invalid, you do not necessarily have to parse it to a long. You can use java regex (regular expressions) to test if the code is negative or zero:

string.matches("-\\d+") // Matches all negative integers, even if they are less than `Long.MIN_VALUE`
string.equals("0") // If the string is zero
string.matches("0+") // If the string consists of a bunch of zeroes, nothing else, or even better
string.matches("0*") // If the string contains nothing except zeroes, meaning this will also match an empty string


So, other than that, this code looks fine to me, but parsing values is fairly expensive and catching exceptions is very expensive, so using regular expressions is probably better.