不需要加throws方法调用处也能捕获异常。
public static void main(String[] args) {
try {
getResult(9, 0);
} catch (Exception e) {
System.out.println("getResult exception.");
}
}
private static int getResult(int i, int n) {
int k;
try {
k = i / n;
} catch (Exception e) {
throw e;
}
return k;
}
执行结果:getResult exception.