【发布时间】:2012-04-25 09:48:43
【问题描述】:
我目前正在学习 Java 入门课程,这是关于 try-catch 方法的。当我输入这个时,我的System.out.println 语句不断重复。这是我的代码:
public static double exp(double b, int c) {
if (c == 0) {
return 1;
}
// c > 0
if (c % 2 == 0) {
return exp(b*b, c / 2);
}
if (c<0){
try{
throw new ArithmeticException();
}
catch (ArithmeticException e) {
System.out.println("yadonegoofed");
}
}
// c is odd and > 0
return b * exp(b, c-1);
}
【问题讨论】:
-
您实际上并没有在这里提出问题。请展开:-)
-
除了特定的问题:抛出异常并立即捕获它并不是很好地使用异常。
标签: java exception-handling if-statement try-catch throw