I have the following problem: In a servlet I need to handle different inputs (in the following Lists) from an already implemented supplier (just the interface is known). This handling only makes sense, if the Lists have the same size (which almost surely is the case, unless the supplier-function messed up), hence I wanted to use assert on the sizes (else it will throw and Index out of Bound Exception or something similar much later, which would be hard to debug for later developers). The problem here is, my program will run on a server without the -ea argument. My question is: Is it still possible to assert the proper way:
try{
List listA = supplier.getListA();
List listB = supplier.getListB();
assert listA.size()==listB.size();
List listC = supplier.getListC();
assert listA.size()==listC.size();
}
catch(AssertionError error){
//error handling
}
or am I thrown back to Exceptions:
try{
List listA = supplier.getListA();
List listB = supplier.getListB();
if(listA.size()!=listB.size())
throw new RuntimeException();
List listC = supplier.getListC();
if(listA.size()!=listC.size())
throw new RuntimeException();
}
catch(RuntimeException exception){
//error handling
}
For the readability the former way is strongly preferred, but wouldn't surely work on the server.
Assert.sameSize(listA, listB)orAssert.equals(listA.size(), listB.size()).logger.put(new RuntimeException, "description");