[英]Regarding try-catch
I am currently taking a introductory course in Java and this is regarding try-catch method. 我目前正在学习Java入门课程,这是关于try-catch方法的。 When I type this my
System.out.println
statement keeps repeating endlessly. 当我键入此内容时,
System.out.println
语句会不断重复。 Here is my code: 这是我的代码:
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);
}
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);
Your comment c is odd and > 0
is incorrect -- you never actually terminated the function with the exception. 您的注释
c is odd and > 0
是不正确的-您从未真正终止过带有异常的函数。 You threw it, you immediately caught it, and then continued to execute the recursive function. 您把它扔了,立即抓住了它,然后继续执行递归函数。 Eventually, when you hit wraparound , it'll be a positive number again, and the errors won't happen.
最终,当您绕回时 ,它将再次为正数,并且不会发生错误。 (It's about two billion iterations away -- don't wait.)
(大约有二十亿次迭代-不要等待。)
I would not use an exception here -- you just need to terminate the recursion. 我不会在这里使用异常-您只需要终止递归即可。 I'd check for negative input before checking for
0
, and throw the exception there, and catch the exception in the caller . 我会先检查是否有负输入, 然后再检查
0
,然后在其中抛出异常,并在调用方中捕获异常 。
In pseudo-code: 用伪代码:
exp(double b, int c) {
if (c < 0)
throw new Exception("C cannot be negative");
} else if (c % 2 == 0) {
return exp(b*b, c / 2);
} else {
/* and so forth */
}
}
You forgot one very important part when it comes to creating your own custom exceptions. 在创建自己的自定义异常时,您忘记了一个非常重要的部分。 You forgot to tell the method that it will throw such a method.
您忘了告诉方法它将抛出这样的方法。 Your first line of code should look like this:
您的第一行代码应如下所示:
public static double exp(double b, int c) throws ArithmeticException {
Note that I have tested this myself, and it will only throw the exception one time in your output. 请注意,我自己对此进行了测试,它只会在您的输出中抛出一次异常。
If for example, c = -1 in, the first and second if fails, the third if throws an exception and then prints the error, but things progress because you handled the excpetion. 例如,如果c = -1 in,则第一个if失败,第二个if失败,第三个if抛出异常,然后输出错误,但是事情进展了,因为您已处理了问题。 So it calls exp(b, -2).
因此它调用exp(b,-2)。 In turn, that calls exp(b, -3) in the return, and so on.
反过来,它在返回中调用exp(b,-3),依此类推。 Add the value of C to your println to verify.
将C的值添加到您的println中进行验证。
Well, right at the end you have return b * exp(b, c-1);
好吧,最后,您将
return b * exp(b, c-1);
this is going to call exp
again which will call it again. 这将再次调用
exp
,它将再次调用它。
So the function will keep repeating and so will System.out.println
. 因此,该函数将继续重复执行,
System.out.println
也将重复执行。
Your BASE case is VERY specific...what in your code GUARANTEES that c will equal 0? 您的BASE案例非常特定...在代码保证中c等于0的情况是什么? Currently, this is the ONLY way that you exit your recursive call.
当前,这是退出递归调用的唯一方法。 As @Jay said you always subtract 1 which causes the thrown exception, but c is already below 0 at that point so it doesn't EQUAL 0. Change your first
if
statement to catch values <= 0 and you should be fine. 正如@Jay所说,您总是减去1导致抛出的异常,但是c在那时已经低于0了,所以它不等于0。更改您的第一个
if
语句以捕获值<= 0,就可以了。
if( c <= 0 )
return 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.