You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化函数内大量异常抛出的代码实现?

优化函数内大量异常检查的方案

问题场景

函数内存在大量独立的异常检查逻辑(占代码量90%),每个检查需抛出带有唯一错误码的异常,且检查逻辑无共性,无法抽取为通用函数单独维护。

示例代码:

public void problematicFunction(String string1, String string2, String string3, String string4) throws GenericException {

    if(string1.someLogicHere()) {
        throw new GenericException("ERR_001", "something is wrong with " + string1);
    }

    if(string2.someLogicHere()) {
        throw new GenericException("ERR_002", "something is wrong with " + string2);
    }

    if(string2.someOtherLogicHere()) {
        throw new GenericException("ERR_003", "something else is wrong with " + string2);
    }

    if(string3.someLogicHere()) {
        throw new GenericException("ERR_004", "something is wrong with " + string3);
    }

    if(string4.someLogicHere()) {
        throw new GenericException("ERR_005", "something is wrong with " + string4);
    }

    mainlogic(string1, string2, string3, string4);
}

优化方案

1. 封装断言式工具类

编写静态工具类抽离「检查逻辑+抛异常」的重复模板,把每个检查简化为一行调用,聚焦核心检查逻辑。

工具类示例:

public class CheckUtils {
    public static <T> void throwIf(T param, Predicate<T> check, String errorCode, String errorMsg) throws GenericException {
        if(check.test(param)) {
            throw new GenericException(errorCode, errorMsg);
        }
    }
}

优化后的函数代码:

public void optimizedFunction(String string1, String string2, String string3, String string4) throws GenericException {
    CheckUtils.throwIf(string1, s -> s.someLogicHere(), "ERR_001", "something is wrong with " + string1);
    CheckUtils.throwIf(string2, s -> s.someLogicHere(), "ERR_002", "something is wrong with " + string2);
    CheckUtils.throwIf(string2, s -> s.someOtherLogicHere(), "ERR_003", "something else is wrong with " + string2);
    CheckUtils.throwIf(string3, s -> s.someLogicHere(), "ERR_004", "something is wrong with " + string3);
    CheckUtils.throwIf(string4, s -> s.someLogicHere(), "ERR_005", "something is wrong with " + string4);

    mainlogic(string1, string2, string3, string4);
}

2. 流式批量检查

定义检查项实体类,将每个检查的参数、逻辑、错误信息打包成对象,通过流式遍历执行所有检查,实现检查逻辑与核心逻辑的分离。

检查项类示例:

// 可使用Lombok的@AllArgsConstructor简化构造,或手动实现
public class CheckItem<T> {
    private T param;
    private Predicate<T> checkLogic;
    private String errorCode;
    private String errorMsg;

    public CheckItem(T param, Predicate<T> checkLogic, String errorCode, String errorMsg) {
        this.param = param;
        this.checkLogic = checkLogic;
        this.errorCode = errorCode;
        this.errorMsg = errorMsg;
    }

    public void execute() throws GenericException {
        if(checkLogic.test(param)) {
            throw new GenericException(errorCode, errorMsg);
        }
    }
}

优化后的函数代码:

public void optimizedFunction(String string1, String string2, String string3, String string4) throws GenericException {
    List<CheckItem<?>> checks = Arrays.asList(
        new CheckItem<>(string1, s -> s.someLogicHere(), "ERR_001", "something is wrong with " + string1),
        new CheckItem<>(string2, s -> s.someLogicHere(), "ERR_002", "something is wrong with " + string2),
        new CheckItem<>(string2, s -> s.someOtherLogicHere(), "ERR_003", "something else is wrong with " + string2),
        new CheckItem<>(string3, s -> s.someLogicHere(), "ERR_004", "something is wrong with " + string3),
        new CheckItem<>(string4, s -> s.someLogicHere(), "ERR_005", "something is wrong with " + string4)
    );

    checks.forEach(CheckItem::execute);

    mainlogic(string1, string2, string3, string4);
}

3. 紧凑化单行检查

如果不想新增工具类或实体类,直接将每个检查逻辑压缩为单行,减少换行带来的视觉冗余,让核心逻辑更突出。

优化后的函数代码:

public void optimizedFunction(String string1, String string2, String string3, String string4) throws GenericException {
    if(string1.someLogicHere()) throw new GenericException("ERR_001", "something is wrong with " + string1);
    if(string2.someLogicHere()) throw new GenericException("ERR_002", "something is wrong with " + string2);
    if(string2.someOtherLogicHere()) throw new GenericException("ERR_003", "something else is wrong with " + string2);
    if(string3.someLogicHere()) throw new GenericException("ERR_004", "something is wrong with " + string3);
    if(string4.someLogicHere()) throw new GenericException("ERR_005", "something is wrong with " + string4);

    mainlogic(string1, string2, string3, string4);
}

内容的提问来源于stack exchange,提问作者Windir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 17:50:44