java中的异常或null

我有下一个疑问.根据java的良好实践,如何管理无法找到对象的情况,我们想知道原因.

例如,如果有人登录我们的系统时遇到问题,并且我们想要确切地告诉他们问题是什么,我们就不能返回null,因为我们失去了无法登录的原因.例如:

public User login(String username, String password) {
    boolean usernameEmpty = (credentials.getUsername()==null || credentials.getUsername().isEmpty());
    boolean passwordEmpty = (credentials.getPassword()==null || credentials.getPassword().isEmpty());
    //getUserPassword return null if doesn't exist an user with username and password return null
    User user = getUserPassword(username,password);

    if (!usernameEmpty && !passwordEmpty && user!=null) {
        LOGGER.info("Found " + username);
    } else if (!usernameEmpty && !passwordEmpty && user==null) {
        LOGGER.info("There is no such username and password: " + username);
    } else if (usernameEmpty) {
        LOGGER.info("Username can not be empty ");
    } else if (passwordEmpty) {
        LOGGER.info("Password can not be empty ");
    }

    return user;
}

我可以想出两个有利有弊的选择来解决它.

第一个是使用异常,但我认为使用不同的方案并不像预期的异常那样好.出于这个原因,我放弃了它.

第二个是将对象(User)包含在另一个对象中以管理不同的可能性.例如,使用以下内容:

public class EntityObject<t> {
    //Is used to return the entity or entities if everything was fine
    private t entity;
    //Is used to inform of any checked exception
    private String exceptionMessage;

    //getters / setters / ..
}

public EntityObject<User> login(String username, String password) {
    boolean usernameEmpty = (credentials.getUsername()==null || credentials.getUsername().isEmpty());
    boolean passwordEmpty = (credentials.getPassword()==null || credentials.getPassword().isEmpty());
    User user = getUserPassword(username,password);
    EntityObject<User> entity = null;

    if (!usernameEmpty && !passwordEmpty && user!=null) {
        LOGGER.info("Found " + username);
        entity = new EntityObject<User>(user);
    } else if (!usernameEmpty && !passwordEmpty && user==null) {
        entity = new EntityObject<User>("There is no such username and password: " + username); 
    } else if (usernameEmpty) {
        entity = new EntityObject<User>("Username can not be empty ");
    } else if (passwordEmpty) {
        entity = new EntityObject<User>("Password can not be empty ");
    }

    return entity;
}

我比第一个选项更喜欢第二个选项,但我不喜欢我必须更改方法签名以返回与通常(用户)不同的类(EntityObject).

通常是什么?它通常如何管理?
非常感谢

解决方法:

对我来说,第二种选择看起来更好.也许,要知道错误是什么而不是在java代码中编写消息,您可以创建包含可能场景的枚举并在前端代码中解析它,如果您确实需要消息,则可以在枚举内创建构造函数来存储它.它将在未来简化对象的支持和工作.此外,添加更多场景不会对您造成太大伤害.

基本版:

public class EntityObject<t> {
    //Is used to return the entity or entities if everything was fine
    private t entity;
    //Is used to inform of any checked exception
    private enum auth {
        NO_PASSWORD, NO_USERNAME, USER_DOES_NOT_EXIST, SUCCESS    
    }
}

带枚举构造函数的版本:

public class EntityObject<t> {
    //Is used to return the entity or entities if everything was fine
    private t entity;
    //Is used to inform of any checked exception
    private enum auth {
        NO_PASSWORD("Password cannot be empty"),
        NO_USERNAME("Username cannot be empty"), 
        USER_OR_PASSWORD_DOES_NOT_EXIST("No such username or password exist"),
        SUCCESS("OK");
        public String message;
        public auth(String message) {
            this.message = message;
        }   
    }
}
上一篇:Java 添加、验证PDF 数字签名


下一篇:android – Google Playstore:使用不同的证书时替换app?