【问题标题】:Uninitialized variable inside the try/catch with unhandled exceptiontry/catch 中未初始化的变量,带有未处理的异常
【发布时间】:2019-04-16 23:29:05
【问题描述】:

我有一个来自异常类的变量要初始化。

因此,如果我执行 ConfigurationProvider reader = configurationReader.configurationProvider() 之类的操作,.configurationProvider() 部分会显示红线,说明 IntelliJ 中未处理异常。

所以我尝试在 try/catch 块中捕获它,如下所示:

    private String getConfigValue(ConfigurationProviderConfiguration configurationReader, String configName) {
        String value = null;
        ConfigurationProvider reader;
        try {
             reader = configurationReader.configurationProvider();
        } catch (Exception e){
            e.printStackTrace();
        }

        Properties config = reader.getConfiguration(configName); //Now, there is a red line under reader saying the variable might not have been initialized
        if (config == null) {
            LOGGER.warn("The configuration for " + configName + " cannot be found.");
        }else{
            value = config.getValue();
            if (value == null) {
                LOGGER.warn("The configuration for " + configName + " cannot be found.");
            }
        }
        return value;
    } 

现在您可以在评论中看到,读者下方有一条红线表示变量未初始化。我理解为什么编译器会抱怨,因为它可能会跳过 try 并转到 catch 块。我会尝试删除 catch 块,但我也不能这样做,因为我必须处理异常。在这种情况下我能做什么?任何帮助将不胜感激。

【问题讨论】:

  • 初始化它:ConfigurationProvider reader = null;
  • 将使用阅读器的代码也放入 try 块中。
  • @CardinalSystem 然后在执行到该行时产生空指针异常。

标签: java variables exception initialization try-catch


【解决方案1】:

有一个执行路径,其中reader 未初始化——如果抛出并捕获异常。如果在尝试初始化时引发异常,您甚至不应该尝试使用 reader

只有在没有抛出异常的情况下,您才应该使用它并尝试返回一个值。将catch块后面的所有代码放入初始化后的try块中。

try {
    reader = configurationReader.configurationProvider();

    Properties config = reader.getConfiguration(configName);
    if (config == null) {
        LOGGER.warn("The configuration for " + configName + " cannot be found.");
    } else {
        value = config.getValue();
        if (value == null) {
            LOGGER.warn("The configuration for " + configName + " cannot be found.");
        }
    }
    return value;
} catch (Exception e){
    e.printStackTrace();
}

现在编译器会抱怨不是所有的路径都返回一个值,在这种情况下抛出异常时这是真的。要么重新抛出异常,要么返回一些东西来表明返回的值是无效的。重新抛出异常还需要在您的方法中使用 throws 子句。

} catch (Exception e){
    e.printStackTrace();
    throw e;
    // OR
    return null;
}

【讨论】:

  • “现在编译器会抱怨不是所有的路径都返回一个值”——你可以把 return value; 行移到 try/catch 之外。
【解决方案2】:

将其余代码移到同一个异常处理程序中:

private String getConfigValue(ConfigurationProviderConfiguration configurationReader, String configName) {
    String value = null;
    ConfigurationProvider reader;
    try {
        reader = configurationReader.configurationProvider();

        Properties config = reader.getConfiguration(configName);
        if (config == null) {
            LOGGER.warn("The configuration for " + configName + " cannot be found.");
        }else{
            value = config.getValue();
            if (value == null) {
                LOGGER.warn("The configuration for " + configName + " cannot be found.");
            }
        }
    } catch (Exception e){
        e.printStackTrace();
    }

    return value;
} 

【讨论】:

  • 谢谢杰森,是的,我不确定谁对你的帖子投了反对票。我昨天投了赞成票以使其公平:)
猜你喜欢
  • 2011-01-01
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-13
  • 1970-01-01
相关资源
最近更新 更多