1

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.

3
  • Initialize it: ConfigurationProvider reader = null; Commented Apr 16, 2019 at 23:35
  • Put your code that uses the reader in the try block too. Commented Apr 16, 2019 at 23:36
  • @CardinalSystem Which then produces a null pointer exception when the execution gets to that line. Commented Apr 16, 2019 at 23:36

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

"Now the compiler will complain that not all paths return a value" - you could move the return value; line outside of the try/catch.
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 Comment

Thanks Jason, yeah I'm not sure who down voted your post. I upvoted it yesterday to make it fair :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.