【发布时间】:2016-02-25 03:51:06
【问题描述】:
我是 Java 新手,正在尝试学习捕获异常的概念。我在网上看到了这段代码,它在另一个 try-catch-finally 块的主体内有一个 try-catch 块。我只是想知道是否有任何方法可以简化代码以便以更清晰的方式编写?
public static void main(String[] args) {
Properties p1 = new Properties();
OutputStream os1 = null;
try {
os1 = new FileOutputStream("xanadu123.properties");
//set the properties value
p1.setProperty("database", "localhost");
p1.setProperty("1", "one");
p1.setProperty("2", "two");
p1.store(os1, "this is the comment");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os1 != null) {
try {
os1.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
【问题讨论】:
标签: java exception exception-handling try-catch