This is the block of code inside the boolean method that I want to check if there are any empty or null fields. It returns true if none of them are. If they are, it throws a custom exception for each case, but it messes up my entire program when it makes me throw the IllegalAccessException.
for(Field f: getClass().getDeclaredFields()) {
f.setAccessible(true);
try {
if((f.get(this)) == null) {
throw new NullValueException("Error, this book has null value");
}
if(f.get(this).toString().isEmpty()) {
throw new ItemEmptyException("Error, this book has null value");
}
}catch (IllegalAccessException e) {
throw e;
}
}
Even if I use f.setAccessible(true)
, it still makes me use try-catch clause for the exception.
Can I avoid that try-catch clause and never have to throw an IllegalAccessException?