简体   繁体   中英

Regarding try-catch

I am currently taking a introductory course in Java and this is regarding try-catch method. When I type this my System.out.println statement keeps repeating endlessly. 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. 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 .

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. So it calls exp(b, -2). In turn, that calls exp(b, -3) in the return, and so on. Add the value of C to your println to verify.

Well, right at the end you have return b * exp(b, c-1); this is going to call exp again which will call it again.
So the function will keep repeating and so will System.out.println .

Your BASE case is VERY specific...what in your code GUARANTEES that c will equal 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.

if( c <= 0 )
     return 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM