I have a method called register which takes a custom class (called RegistrationRequest) as a parameter and there are a few variables in RegistrationRequest which are required and cannot be null (url and type). The register method saves the registration in DynamoDB. However, I want to catch a custom exception if either of the 2 required variables are null and avoid saving such entries in DynamoDB. The code block for this method is somewhat like this
public Registration register(RegistrationRequest registration)
var dbEntry = RegistrationImpl.builder()
.withType(registration.getType())
.withDescription(registration.getDescription())
.withUrl(registration.getUrl())
.withCategory(registration.getCategory())
.withMaxDuration(registration.getMaxDuration())
.build();
try {
return repository.save(dbEntry);
}
catch (Exception e) {
......
}
I want to have another catch block which checks that registration.type and registration.url are not null and if they are, then catch that and throw an exception and therefore prevent saving such entries in DynamoDB. How do I do that?
The validation can be performed just when you enter the method. Something like this: