2

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); } 
2
  • 1
    You haven't actually asked a question here. Please expand :-) Commented Apr 13, 2012 at 0:53
  • 2
    Apart form the particular problem: Throwing an exception and catching it immediatly is not a good use of exceptions. Commented Apr 13, 2012 at 1:00

5 Answers 5

3
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 */ } } 
Sign up to request clarification or add additional context in comments.

6 Comments

You explain it far better than I did below. To be fair, if having a negative c truly is an exceptional situation (i.e. c should never be < 0), it may be sensible to throw - but you certainly shouldn't be catching the exception you just threw.
@James: agreed, catching the exception immediately is very strange. I have seen exceptions used to exit deeply-nested loops before, but it is odd practice for a method to catch an exception it throws.
and looking back at the code, it's only valid for positive integers, so it makes sense to throw, but it should definitely be propagating it, not catching.
it's still odd to use there. I'd think a labeled continue would be more appropriate.
I think throwing an exception is fair game -- since this function is only valid for non-negative inputs, any caller that calls it with a non-negative input is, by definition, broken.
|
2

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.

1 Comment

This method also catches the exception -- it can't leave the function. It's a good point though, once the problem is fixed. :)
1

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.

2 Comments

I especially like the suggestion Add the value of C to your println to verify.. That will point out the problem very quickly. :)
@sarnold - my general principle of throwing exceptions - if it's truly an exceptional case, and you can't tell why it's exceptional from the message, you're doing it wrong :)
0

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.

Comments

0

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; 

2 Comments

While I think you hit this quite well, the case that c < 0 is probably an error, and should be handled as an error.
@sarnold while this may be true. there is no actual questions so everyone is simply speculating as to what the expected result is... The only thing he seems to mention is a problem is that the sys.out always prints. I simply stated a way to solve that. Again, with the info given all any of us can do is speculate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.