我在java中有这个工厂方法:
public static Properties getConfigFactory() throws ClassNotFoundException, IOException {
if (config == null) {
InputStream in = Class.forName(PACKAGE_NAME).getResourceAsStream(CONFIG_PROP);
config = new Properties();
config.load(in);
}
return config;
}
Run Code Online (Sandbox Code Playgroud)
我想将两个已检查的异常转换为未经检查的异常.最好的方法是什么?
我应该捕获异常并使用捕获的异常作为内部异常抛出一个新的RuntimeException吗?
有没有更好的方法来做到这一点,或者我是否应该首先尝试这样做?
编辑:
只是为了澄清.这些异常将是致命的,因为配置文件基本上是程序的操作,所有异常都将被捕获并记录在我的程序的顶层.
我的目的是避免不必要的抛出异常,将异常添加到调用我的工厂的每个方法的签名中.
try {
someMethodThatCouldThrowAnything();
} catch (IKnowWhatToDoWithThisException e) {
handle(e);
} catch (Throwable t) {
Throwables.propagateIfInstanceOf(t, IOException.class);
Throwables.propagateIfInstanceOf(t, SQLException.class);
throw Throwables.propagate(t);
}
Run Code Online (Sandbox Code Playgroud)
不是很具体.真正的程序会是什么样子?我真的不明白的方法的目的Throwables.propagateIfInstanceOf(Throwable, Class),propagate(),propagateIfPossible().我什么时候使用它们?