我有一个调用具有默认构造函数参数的构造函数的问题。使用try/catch内部调用另一个构造函数的构造函数
Class A {
private static Properties properties;
A(Properties property){
// do some checks
try{
load(property, fileName)
} catch(IOException e) {
throw new RuntimeException();
}
}
A(){
this(load(properties));
}
private static Properties load(Properties properties, String fileName) throws IOException {
try {
properties.load(A.class.getClassLoader()
.getResourceAsStream(fileName));
} catch (IOException ioException) {
throw new IOException("Unable to process the properties File. " + fileName, ioException);
}
return properties;
}
}
我的问题是:在默认的构造函数中,我想使用try/catch块,并执行抛出运行时异常的相同操作。你能帮我解决这个问题吗?
WRT这个帖子:chaining constructors in Java without throwing exceptions from the default constructor
我把try/catch语句内的另一个方法的选择。但有没有其他方法? PS:我不想用“抛出”
你究竟在做什么?你能清理一下你的文章吗? –
我试图有两个构造函数。一个带有参数和一个,默认值。如果我的类的使用者没有调用参数构造函数,那么我需要自动加载并将其传递给带有参数的构造函数 – Vikram
发布加载函数 – Joel