代码之家  ›  专栏  ›  技术社区  ›  Mert Yücel

如何在布尔方法中引发三个异常?

  •  1
  • Mert Yücel  · 技术社区  · 8 年前

    我是Java世界的新手,我试图理解异常,但我没有得到的是; 如何在布尔方法中引发异常? 当我必须在一个捕获中使用三个异常时,该怎么办?

    @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...
    
            }
        }
    

    或者我可以做到这一点,但它也帮不了我:我不知道如何在布尔方法中使用异常。

    @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");
            }
        }
    
    4 回复  |  直到 8 年前
        1
  •  1
  •   Elliott Frisch    8 年前

    由于方法 isValid 签名中不包含任何例外。出于教学目的,让我们定义一个自定义异常类型:

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

    然后我们可以添加 throws MyException 至方法签名 isValid有效 然后把它扔进 multi-catch 。喜欢

    @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);
        }
    }
    

    如果你想要这些 具体的 返回调用堆栈的异常-只需将它们添加到抛出行并删除 try-catch (或在 尝试捕捉 如果你 真正地 出于某种原因,希望在此处登录)。

    @Override
    public boolean isValid(Object bean, ConstraintValidatorContext ctx)
            throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // No try-catch. Otherwise the same.
    }
    
        2
  •  1
  •   Yura Miroshnichenko    8 年前

    有两种处理异常的方法:

    • 现在做点什么
    • 扔掉它(稍后再处理)

    据我所知,您不能在那里引发异常,因为您的方法不允许引发异常,所以您应该处理此方法中所有已检查的异常。或者您可以添加关键字“throws”:

    isValid(...) throws NoSuchMethodException {
    
    ...
    
    throw e;
    
    }
    

    这将允许您抛出类NoSuchMethodException的异常。

        3
  •  0
  •   huanjinzi    8 年前

    您只想捕获预期的三个异常,但在java中,任何未捕获或抛出的异常,java应用程序都会崩溃。你这样做是胡说八道

        4
  •  0
  •   TheJavaGuy-Ivan Milosavljević    8 年前

    这里的问题是您的类正在实现 javax.validation.ConstraintValidator 。方法的签名 isValid 在该接口中定义为 boolean isValid(T value, ConstraintValidatorContext context); 。这就是为什么你不能从你的实现中抛出一个选中的异常——你会违反接口。否则,如果您在一个类中实现一个方法,而该类没有实现任何接口,或者实现了一些可以更改的接口,那么您可以随意更改方法签名并抛出异常。

    推荐文章