Uninitialized variable inside the try/catch with unhandled exception

时间:2019-04-16 23:29:05

标签: java variables exception initialization try-catch

I have a variable to initialize that is coming from a class with exception.

So if I do something like ConfigurationProvider reader = configurationReader.configurationProvider(), the .configurationProvider() part shows the red line telling the exception is unhandled in IntelliJ.

So I tried catching it inside the try/catch block like the following:

    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;
    } 

Now as you can see in the comment, there is a red line under reader saying the variable is not initialized. I understand why the compiler is complaining since it may skip the try and go to the catch block. I would try deleting catch block but I also can't do that since I have to handle the exception. What can I do in this case? Any help would be greatly appreciated.

2 个答案:

答案 0 :(得分:1)

Move the rest of your code inside the same exception handler:

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;
} 

答案 1 :(得分:1)

There is a path of execution where reader is not initialized -- if an exception is thrown and caught. It looks like you shouldn't even try to use reader if an exception was thrown attempting to initialize it.

Only if an exception wasn't thrown should you use it and attempt to return a value. Place all the code following the catch block into the try block following the initialization.

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();
}

Now the compiler will complain that not all paths return a value, which is true in this case when the exception is thrown. Either re-throw the exception or return something to indicate that the returned value was invalid. Re-throwing the exception will also require a throws clause on your method.

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