Discuss / Java / 看多了,思考就多了.

看多了,思考就多了.

Topic source

🌙

#1 Created at ... [Delete] [Delete and Lock User]

多个catch 语句

是否是设置多个变量依次判断添加呢?

这样就感觉有点蠢啦~~~

有兴趣的同学可以试试,我要回家啦~~~~

🌙

#2 Created at ... [Delete] [Delete and Lock User]

自己挖的坑自己填

public class Test04 {
public static void main(String[] args) throws Exception {
Exception origin = null;
try {
System.out.println(Integer.parseInt("abc"));
} catch (NumberFormatException e) {
origin = e;
throw e;
} catch (Exception e) {
origin = e;
throw e;
} finally {
Exception e = new IllegalArgumentException();
if (origin != null) {
e.addSuppressed(origin);
}
throw e;
}
}
}

不知道还可不可以优化...100个catch 要写100次么?

Joker.fu_95

#3 Created at ... [Delete] [Delete and Lock User]

找到一个异常后面再多catch块都不会被执行执行

🌙

#4 Created at ... [Delete] [Delete and Lock User]

楼上,的,虽然我也忘了,我当时怎么想的了。。。但是我确定你,说得和我想的,不是一回事

🌙

#5 Created at ... [Delete] [Delete and Lock User]

我的意思应该是,为什么避免写重复代码,有了finally,难道不应该有个addAllSuppressedfan方法么?

🌙

#6 Created at ... [Delete] [Delete and Lock User]

想的有点天真,现在看,还是很清晰的,这一节真是nice


  • 1

Reply