【发布时间】:2016-08-26 05:18:48
【问题描述】:
以下两种方法有区别吗?
哪个更好,为什么?
Prg1:
public static boolean test() throws Exception {
try {
doSomething();
return true;
} catch (Exception e) {
throw new Exception("No!");
}
}
Prg2:
public static boolean test() throws Exception {
try {
doSomething();
} catch (Exception e) {
throw new Exception("No!");
}
return true;
}
【问题讨论】:
-
我更喜欢第二个 sn-p,因为我觉得它更干净(更清晰)。我认为这不会影响性能。
-
我更喜欢第一个,因为如果你决定在本地处理异常而不是重新抛出它会发生什么。