4

I have following code:

public class LoadProperty
{
public static final String property_file_location = System.getProperty("app.vmargs.propertyfile");
public static final String application-startup_mode = System.getProperty("app.vmargs.startupmode");
}

It reads from 'VM arguments' and assigns to variables.

Since static final variable is only initialized at class load, how can I catch exception in case some one forgets to pass parameter.

As of now, when I am using 'property_file_location' variable, exception is encountered in following cases:

  • If value is present, and location is wrong, FileNotFound exception comes.
  • If it is not initialized properly(value is null), it throws NullPointerException.

I need to handle second case at time of initialization only.

Similiar is case of second variable.

Whole idea is

  • To initialize application configuration parameters.
  • If successfully initialized, continue.
  • If not, alert user and terminate application.

3 Answers 3

5

You can catch it this way:

public class LoadProperty
{
    public static final String property_file_location;

    static {
        String myTempValue = MY_DEFAULT_VALUE;
        try {
            myTempValue = System.getProperty("app.vmargs.propertyfile");
        } catch(Exception e) {
            myTempValue = MY_DEFAULT_VALUE;
        }
        property_file_location = myTempValue;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The last asignation property_file_location = myTempValue;must be inside a finally block
2

You can use a static initializer block as suggested by the rest of the answers. Even better move this functionality to a static utility class so you can still use them as an one-liner. You could then even provide default values e.g.

// PropertyUtils is a new class that you implement
// DEFAULT_FILE_LOCATION could e.g. out.log in current folder
public static final String property_file_location = PropertyUtils.getProperty("app.vmargs.propertyfile", DEFAULT_FILE_LOCATION); 

However if those properties are not expected to exist all the time, I would suggest to not initialize them as static variables but read them during normal execution.

// in the place where you will first need the file location
String fileLocation = PropertyUtils.getProperty("app.vmargs.propertyfile");
if (fileLocation == null) {
    // handle the error here
}

1 Comment

Above parameters were subset of actual. I have parameters which I am using all the time. This aproach should work for me. PropertyUtils.getProperty(), will give me way to do exception handling. I will try.
0

You may want to use a static bloc :

public static final property_file_location;
static {
  try {
    property_file_location = System.getProperty("app.vmargs.propertyfile");
  } catch (xxx){//...}
}

2 Comments

That's because if it fails your final variable isn't set. so you could add a finally {proeprty_file_location = null};
Even with a finally clause, have a try and compile it.

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.