user6941415
user6941415

Reputation:

How to throw three exception in boolean method?

I am new to the Java World and I am trying to understand exceptions but what I didnt get is; How can I throw an exception in boolean method? And what to do when I have to use three Exceptions in one catch?

@Override
    public boolean isValid(Object bean, ConstraintValidatorContext ctx) {
        try {
            if (Assert.isNull(bean)) {
                logger.info(EXC_MSG_BEAN_NULL, bean.toString());
            }

            String dependentFieldActualValue;
            dependentFieldActualValue = BeanUtils.getProperty(bean, dependentField);
            boolean isActualEqual = stringEquals(dependentFieldValue, dependentFieldActualValue);

            if (isActualEqual == ifInequalThenValidate) {
                return true;
            }
            return isTargetValid(bean, ctx);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            logger.info("Necessary attributes can't be accessed: {}", e.getMessage());
//I cant throw an exception here...

        }
    }

or i can do this but it didnt also helped me: I have no idea how to use exception in boolean method.

@Override
    public boolean isValid(Object bean, ConstraintValidatorContext ctx) {
        try {
            if (Assert.isNull(bean)) {
                logger.info(EXC_MSG_BEAN_NULL, bean.toString());
            }

            String dependentFieldActualValue;
            dependentFieldActualValue = BeanUtils.getProperty(bean, dependentField);
            boolean isActualEqual = stringEquals(dependentFieldValue, dependentFieldActualValue);

            if (isActualEqual == ifInequalThenValidate) {
                return true; 
            }
            return isTargetValid(bean, ctx); 
        } catch (ReflectiveOperationException e) {
            logger.info("Necessary attributes can't be accessed: {}", e.getMessage());
            //Here should be my throw new ReflectiveOperationException("ERROR");
        }
    }

Upvotes: 1

Views: 8040

Answers (4)

The problem here is that your class is implementing javax.validation.ConstraintValidator. The signature of the method isValid is defined in that interface as boolean isValid(T value, ConstraintValidatorContext context);. That's why you can't throw a checked exception from your implementation - you would violate the interface. Otherwise, if you were implementing a method in a class which doesn't implement any interface, or implements some interface you can change, you'd be able to change the method signature and throw exceptions at will.

Upvotes: 0

huanjinzi
huanjinzi

Reputation: 1

You only want to catch the three exceptions you expected,but in java ,any exception you don’t catch or throw it,the java application will crash.you do like this is nonsense

Upvotes: 0

Yura Miroshnichenko
Yura Miroshnichenko

Reputation: 91

There are 2 ways to work with exception:

  • Do something with it now
  • Throw it (do something with it later)

As I understand you can't throw exception there because your method doesn't allow to throw exception, so you should deal with all checked exceptions within this method. Or you can add keyword "throws":

isValid(...) throws NoSuchMethodException {

...

throw e;

}

and this would allow you to throw exception of class NoSuchMethodException.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

You can't throw an exception at the indicated point because the method isValid doesn't include any Exception's in the signature. For pedagogical purposes, let's define a custom exception type:

static class MyException extends Exception {
    public MyException(Exception e) {
        super(e);
    }
}

And then we can add throws MyException to the method signature of isValid and actually throw it in the multi-catch. Like,

@Override
public boolean isValid(Object bean, ConstraintValidatorContext ctx) throws MyException {
    try {
        // ...
    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        logger.info("Necessary attributes can't be accessed: {}", e.getMessage());
        throw new MyException(e);
    }
}

If you want those specific exceptions to return up the call stack - just add them to the throws line and remove the try-catch (or re-throw in the try-catch if you really want to log here for some reason).

@Override
public boolean isValid(Object bean, ConstraintValidatorContext ctx)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    // No try-catch. Otherwise the same.
}

Upvotes: 0

Related Questions