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块都不会被执行执行


  • 1

Reply