DEFENSIVE PROGRAMMING WITH KLOJANG CHECK
Error Handling
When a value fails to pass a test, an error message needs to be generated and an exception needs to be thrown. You have three options here:
- Klojang Check generates both the exception and the exception message
- Klojang Check generates the exception and you provide the exception message
- You do both
Here is an example of each of the three variants:
Check.that(vehicle, "vehicle").is(instanceOf(), Car.class);
Check.that(vehicle).is(instanceOf(), Car.class, "bikes are not for rent here");
Check.that(vehicle).is(instanceOf(), Car.class,
() -> new RentalException("bikes are not for rent here"));
The first variant really is meant to be used only in combination with a check from the CommonChecks class. As explained in the chapter on this class, if you provide your own lambda or method reference, but you don't provide your own error message, Klojang Check will generate an error message for you, but it will not be very helpful.