How to properly throw MethodArgumentNotValidException
If you have lombok
dependency in your project, you can also fake compiler by using @SneakyThrows
annotation.
https://projectlombok.org/features/SneakyThrows
MethodArgumentNotValidException
is a subclass of Exception
. This means that it's "checked": To throw it out of your verifyCard(..)
method, you have to declare that verifyCard(..)
can throw it:
private void verifyCard(CardRequest card) throws MethodArgumentNotValidException {
// your code
}
You have already handled it by the catch
block, you should remove try-catch
to your global handler catch it.
then specify the method like below
private void verifyCard(CardRequest card) throws MethodArgumentNotValidException