[英]Should I use try-catch or throws custom exception or both while reading property file in Java?
I am reading properties from property file in the classpath of my project. 我正在从项目的类路径中的属性文件读取属性。
public Properties readFileFromClasspath(String fileName) throws MyException {
Properties props = new Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
if (inputStream != null) {
try {
props.load(inputStream);
} catch (IOException e) {
throw new MyException("Could Not read properties from file : [" + fileName + "].",e);
}
} else {
throw new MyException("Could Not find property file : [" + fileName + "] in the classpath.");
}
return mainProperties;
}
Here MyException
simply extends Exception
. 在这里,
MyException
只是扩展了Exception
。
Should I use try-catch or throws MyException or both ? 我应该使用try-catch还是抛出MyException或同时 抛出 两者 ?
Depends. 要看。 Can you load default properties if there is an exception?
如果有异常,是否可以加载默认属性? In that case catch the exception, load default properties and warn in a log.
在这种情况下,请捕获异常,加载默认属性并在日志中发出警告。
If there is impossible to load application without properties, you must thrown an exception. 如果没有属性就无法加载应用程序,则必须引发异常。
You have to decide whether your exceptions can be controled or not. 您必须决定是否可以控制您的异常。
I think you are free to choose, but in both cases, you should always be as much informative as possible when throwing. 我认为您可以自由选择,但是在两种情况下,投掷时都应该尽可能多地提供信息。 In this case, I see no reason to create a custom exception.
在这种情况下,我认为没有理由创建自定义异常。 I'd throw
IOException
if something bad happens while reading the properties file, otherwise FileNotFoundException
if the properties file is not there. 如果在读取属性文件时发生错误,我会抛出
IOException
,否则如果属性文件不存在,则抛出FileNotFoundException
。 If you want to read more, here is one of the many tutorials you can find about exception handling. 如果您想了解更多信息, 这里是您可以找到的许多有关异常处理的教程之一。
I think sometimes we over do it with the exceptions, to me your code should only verify the file exists and throw a FileNotFoundException, other than that why throw the exception that is already being thrown? 我认为有时候我们会用异常来结束它,对我来说,您的代码应该只验证文件存在并抛出FileNotFoundException,而不是为什么抛出已经抛出的异常? You're not doing anything special in the catch.
您在捕获中没有做任何特别的事情。
Assuming you are loading application properties during startup from a fixed-name properties file I would throw an undeclared, unchecked exception: 假设您在启动期间从固定名称的属性文件中加载应用程序属性,我将抛出未声明,未经检查的异常:
public void readApplicationProperties() {
this.properties = readFileFromClasspath("application.properties");
}
private Properties readFileFromClasspath(String fileName) {
Properties props = new Properties();
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(fileName);
if (inputStream == null) {
throw new RuntimeException("Could not find property file: [" + fileName + "] in the classpath.");
}
try {
props.load(inputStream);
} catch (IOException e) {
throw new RuntimeException("Could not read properties from file: [" + fileName + "].", e);
}
return props;
}
My reasoning is that the application cannot recover from this error so there is no point creating a custom checked exception, catching it, etc. In the end the application will have to terminate with a meaningful error message. 我的理由是,应用程序无法从此错误中恢复,因此没有必要创建自定义已检查的异常,将其捕获等。最后,应用程序将不得不终止并显示有意义的错误消息。 The simplest way to do this is with an unchecked exception.
最简单的方法是使用未经检查的异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.