Java抛出错误的异常

时间:2019-06-07 16:53:17

标签: java spring-boot exception

我创建了2个自定义异常,以处理创建新用户并将其持久化到数据库的情况。电子邮件(用户名)是唯一ID,因此,如果重复电子邮件,则应抛出异常,因为唯一ID已经存在。 我也在进行密码确认匹配。此确认匹配还将引发密码不匹配的自定义异常。这两个部分可以正确地彼此独立地工作,但是,当我将所有内容放在一起进行测试时,如果密码确认失败,则抛出用户名已存在异常而不是密码不匹配异常。为什么?

我尝试重新排序代码,但这似乎无关紧要。我还尝试了if / else,而不是if if,但还是得到了相同的结果

        //Username(email) must be unique
        try {
            //password and confirm password must match
            if (!newGcUser.getPassword().equals(newGcUser.getConfirmPassword())) {
                throw new PasswordMatchException("password and confirm password does not match");
            } 
                //if passwords match - persist to DB
                newGcUser.setPassword(bCryptPasswordEncoder.encode(newGcUser.getPassword()));
                //Do NOT persist or show the confirm Password
                newGcUser.setConfirmPassword("");
                //set user
                newGcUser.setName(newGcUser.getUsername());
                return userRepository.save(newGcUser);

        } catch (Exception e) {
            throw new UsernameAlreadyExistsException("Username: '" + newGcUser.getUsername() + "' already exists.");
        }

    } 

我正在使用邮递员进行测试。 如果我测试了一封电子邮件,但我知道该电子邮件未注册且密码不匹配,则会收到UsernameAlreadyExistsException消息,而不是PasswordMatchException

3 个答案:

答案 0 :(得分:2)

之所以会发生这种情况,是因为您的try {} catch (Exception e) {}块正在捕获要在该块中引发的异常,将异常抛出到try catch块之外并且应该可以捕获该异常:

    // password and confirm password must match
    if (!newGcUser.getPassword().equals(newGcUser.getConfirmPassword())) {
        throw new PasswordMatchException("password and confirm password does not match");
    }

    // Username(email) must be unique
    try {
        // if passwords match - persist to DB
        newGcUser.setPassword(bCryptPasswordEncoder.encode(newGcUser.getPassword()));
        // Do NOT persist or show the confirm Password
        newGcUser.setConfirmPassword("");
        // set user
        newGcUser.setName(newGcUser.getUsername());
        return userRepository.save(newGcUser);

    } catch (Exception e) {
        throw new UsernameAlreadyExistsException("Username: '" + newGcUser.getUsername() + "' already exists.");
    }

(或捕获一个不太通用的异常,例如从userRepository.save抛出的异常并重新抛出该异常,那么它将仅捕获该异常,而不是所有异常)

答案 1 :(得分:0)

之所以发生这种情况,是因为您的PasswordMatchException扩展了Exception,而您的catch块正在捕获它并抛出了UsernameAlreadyExistsException

简化代码以说明我的观点:

try {
     throw new PasswordMatchException();
} catch(Exception e) {
     throw new UsernameAlreadyExistsException();
}

在不了解您的代码可以引发哪种异常的情况下,您可能有两种解决方案:

1)捕获比Exception更具体的内容。

2)将密码检查移到try / catch块之外。

答案 2 :(得分:0)

建议采用不同的方法并将其模块化:

private void matchPassword(..newGcUser..) throws PasswordMatchException{
// password and confirm password must match
    if (!newGcUser.getPassword().equals(newGcUser.getConfirmPassword())) {
        throw new PasswordMatchException("password and confirm password does not match");
    }
}

持久化方法应捕获特定异常:

// Username(email) must be unique
try {
    // if passwords match - persist to DB
   ...
    return userRepository.save(newGcUser);

} catch (DataIntegrityViolationException e) {
    throw new UsernameAlreadyExistsException("Username: '" + newGcUser.getUsername() + "' already exists.");
}
相关问题