I have this factory method in 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;
}
And I want to transform the two checked exceptions into unchecked exceptions. What is the best way to go about this?
Should I just catch the exception and throw a new RuntimeException using the caught exception as the inner exception?
Is there a better way to do this or should I even be attempting to do this in the first place?
EDIT:
Just to clarify. These exceptions will be fatal, as the configuration file is essentially to the operation of the program and all exception will be caught and logged at the top level of my program.
My purpose is to avoid an unnecessary throws exception, exception added to the signature of every method that calls my factory.
callUnchecked(() -> getConfigFactory())in your code and definecallUncheckedsimilar totry { return supplier.get(); } catch (Throwable ex) { throw new RuntimeException(ex); }. This way you state explicit in the caller code that the call might fail and cause a crash, but you do not bloat the code too much. Obviously, this is Java 8 only because of the lambda.