Lea*_*ore 4 java static final exception
我有以下代码:
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");
}
Run Code Online (Sandbox Code Playgroud)
它从“VM 参数”读取并分配给变量。
由于静态最终变量仅在类加载时初始化,因此如果有人忘记传递参数,我如何捕获异常。
截至目前,当我使用“property_file_location”变量时,在以下情况下会遇到异常:
我只需要在初始化时处理第二种情况。
第二个变量的情况类似。
整个想法是
你可以这样捕捉它:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2740 次 |
| 最近记录: |