try/catch 内未初始化的变量,有未处理的异常
Uninitialized variable inside the try/catch with unhandled exception
我有一个变量要初始化,它来自 class 异常。
因此,如果我执行 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;
}
现在正如您在评论中看到的那样,reader 下方有一条红线表示变量未初始化。我理解为什么编译器会抱怨,因为它可能会跳过 try 并转到 catch 块。我会尝试删除 catch 块,但我也不能这样做,因为我必须处理异常。在这种情况下我能做什么?任何帮助将不胜感激。
将其余代码移到同一个异常处理程序中:
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;
}
有一条执行路径 reader
未初始化 - 如果抛出并捕获了异常。看起来你甚至不应该尝试使用 reader
如果抛出异常试图初始化它。
只有在没有抛出异常的情况下,您才应该使用它并尝试 return 一个值。将 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();
}
现在编译器会抱怨不是所有路径 return 都有一个值,在本例中抛出异常时也是如此。要么重新抛出异常,要么 return 表明 returned 值无效。重新抛出异常还需要在您的方法中使用 throws
子句。
} catch (Exception e){
e.printStackTrace();
throw e;
// OR
return null;
}
我有一个变量要初始化,它来自 class 异常。
因此,如果我执行 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;
}
现在正如您在评论中看到的那样,reader 下方有一条红线表示变量未初始化。我理解为什么编译器会抱怨,因为它可能会跳过 try 并转到 catch 块。我会尝试删除 catch 块,但我也不能这样做,因为我必须处理异常。在这种情况下我能做什么?任何帮助将不胜感激。
将其余代码移到同一个异常处理程序中:
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;
}
有一条执行路径 reader
未初始化 - 如果抛出并捕获了异常。看起来你甚至不应该尝试使用 reader
如果抛出异常试图初始化它。
只有在没有抛出异常的情况下,您才应该使用它并尝试 return 一个值。将 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();
}
现在编译器会抱怨不是所有路径 return 都有一个值,在本例中抛出异常时也是如此。要么重新抛出异常,要么 return 表明 returned 值无效。重新抛出异常还需要在您的方法中使用 throws
子句。
} catch (Exception e){
e.printStackTrace();
throw e;
// OR
return null;
}