Google Guava is a java library with lot of utilities and reusable components.
This requires the library guava-10.0.jar to be in classpath.
The following example shows using Preconditions.checkState() API.
It throws IllegalStateException if the given expression evaluates to false.
//Throws IllegalStateException - if expression is false
//Preconditions.checkState(expression, errorMessage)
Student student = new Student("Sriram", 2, "Chess");
Student student2 = new Student("Charan", 8, "Cricket");
/**
* Negative case
*/ try {
Preconditions.checkState(student2.isKid(),
"This program requires Students below 5 years, but found %s", student2.getAge());
} catch (IllegalStateException e) {
System.out.println(e);
}
/**
* Positive case
*/
Preconditions.checkState(student.isKid(),
"This program requires Students below 5 years, but found %s", student.getAge());
System.out.println("Thanks for providing valid input");
}
}
It gives the following output,
java.lang.IllegalStateException: This program requires Students below 5 years, but found 8
Thanks for providing valid input