异常处理使代码逻辑不清楚

时间:2014-04-08 08:19:00

标签: java exception

我有以下代码,负责为用户分配角色和组。

private void override(List<Assignment> assignments) {
    removeAll();
    addMultiple(assignments);
}

protected void removeAll() {
    removeAllRoles();
    removeAllGroups();
}

private void removeAllGroups() {
    Iterator<String> userGroups = user.getParentGroups(false);
    while (userGroups.hasNext()) {
        UMHelper.removeUserFromGroup(user.getUniqueID(), userGroups.next());
    }
}

private void addMultiple(List<Assignment> assignments) {
    for (Assignment assignment : assignments) {
        add(assignment);
    }
}

public static void addUserToGroup(String userId, String groupId) {
    try {
            SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Trying to add user " + userId + " to group " + groupId);
            groupFactory.addUserToGroup(userId, groupId);
            SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Success");
        } catch (UMException e) {
            SimpleLogger.traceThrowable(Severity.ERROR, loc, "Addition failed", e);
        }
}

我希望逻辑非常明确。大多数代码都是对集合进行迭代。将用户添加到角色或组可能会导致异常,我在日志中报告。

由于我发现抑制异常并不好,因为调用override方法的客户端应该知道分配的结果,所以我重写了添加异常处理的代码。 执行应该继续,即使某些分配失败。

private void override(List<Assignment> assignments) {
    SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Override was started with " + assignments.size() + " assignments");

    try {
        removeAll();
    } catch (UMException e) {
        SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Removing all existing elements failed");
    }

    try {
        addMultiple(assignments);
    } catch (UMException e) {
        SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Adding new elements failed");
    }
}

protected void removeAll() throws UMException {
    boolean successfulRemoval = true;

    try {
        removeAllRoles();
    } catch (UMException e) {
        successfulRemoval = false;
    }

    try {
        removeAllGroups();
    } catch (UMException e) {
        successfulRemoval = false;
    }

    if (!successfulRemoval){
        throw new UMException("Not all user elements could be removed");
    }
}

private void removeAllGroups() throws UMException {
    boolean removeSuccessful = true;

    Iterator<String> userGroups = user.getParentGroups(false);
    while (userGroups.hasNext()) {
        try {
            UMHelper.removeUserFromGroup(user.getUniqueID(), userGroups.next());
        } catch (UMException e) {
            removeSuccessful = false;
        }
    }

    if (!removeSuccessful) {
        throw new UMException("Not all user groups could be removed");
    }
}

private void addMultiple(List<Assignment> assignments) throws UMException {
    boolean additionSuccessful = true;

    for (Assignment assignment : assignments) {
        try {
            add(assignment);
        } catch (UMException e) {
            additionSuccessful = false;
        }
    }

    if (!additionSuccessful) {
        throw new UMException("Addition of new rights failed");
    }
}

public static void addUserToGroup(String userId, String groupId) throws UMException {
        SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Trying to add user " + userId + " to group " + groupId);
        groupFactory.addUserToGroup(userId, groupId);
        SimpleLogger.log(Severity.INFO, Category.APPLICATIONS, loc, null, "Success");
}

现在代码增加了3倍,并没有像现在这样清晰。如果我只是在第一次分配失败后不得不停止执行,代码会更容易,但这是要求。我是否处理了异常错误或有没有办法改进代码?

2 个答案:

答案 0 :(得分:2)

在这种情况下,我会将所有这些方法从抛出异常改为
返回一个布尔值,表示他们是否成功完成了工作 如果您可以控制这些方法并且可以进行此更改,我认为这更好 适合您的场景。

答案 1 :(得分:2)

通过一些代码重组,即所有删除/添加/等都在一个事务中,代码可以更清晰,例如:

String failmsg = null;

// tx is some transaction object
tx.start();
try {
    failmsg = "user removal failed";
    tx.removeUsers();
    failmsg = "group removal failed";
    tx.removeGroups();
    failmsg = "new additions failed";
    tx.addNew();
    tx.commit();
} catch (UMException e) {
    tx.rollback();
    log(failmsg);
} finally {
    tx.close();
}