notNaN()
is a Validate
class that is used to check that the specified double
value is not NaN
. If the specified value is NaN
, the method throws an exception. Otherwise, it does not return anything.
Validate
The definition of Validate
can be found in the Apache Commons Lang
package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-lang
package, refer to the Maven Repository.
You can import the Validate
class as follows.
import org.apache.commons.lang3.Validate;
public static void notNaN(final double value, final String message, final Object... values)
final double value
: The double value to check.
final String message
: The exception message.
final Object... values
: The optional values for the formatted exception message.
This method does not return anything.
import org.apache.commons.lang3.Validate;public class Main{public static void main(String[] args){// Example 1double doubleToValidate = 234322343;Validate.notNaN(doubleToValidate);// Example 2String exceptionMessageFormat = "Argument should not be NaN";doubleToValidate = Double.NaN;Validate.notNaN(doubleToValidate, exceptionMessageFormat);}}
The output of the code will be as follows:
Exception in thread "main" java.lang.IllegalArgumentException: Argument should not be NaN
at org.apache.commons.lang3.Validate.notNaN(Validate.java:907)
at Main.main(Main.java:13)
double value = 234322343
The method does not throw an exception, as the input value is not NaN
.
double value = Double.NaN
The method throws an IllegalArgumentException
exception, as the input value is NaN
.