0

Why I am not getting any Exception in the following code? After running this code I am getting an infinite loop mentioning at test.fact(t.java:32) No Compile-Time Error was found.

class test
{

    int fact(int m) throws Exception
    {
        if (m==1)
        {
        return 1;
        }
    else
        return (fact ((m-1)*m));
    }
}

class main
{
    public static void main(String ar[]) throws Exception
    {
        test t = new test();
        System.out.println(t.fact(5));
    }
}

while say for example i am using

return(a+b); 

it executes successfully whats the problem with the recursion to show an error???

Rand Mate
  • 453
  • 3
  • 8
  • 14
  • 1
    You're not seeing a StackOverflowError? – Hovercraft Full Of Eels Oct 07 '12 at 15:48
  • 1
    @HovercraftFullOfEels sorry this is off topic but I love your name – Jakob Bowyer Oct 07 '12 at 15:49
  • 1
    @JakobBowyer and i liked your comment. – Juvanis Oct 07 '12 at 15:49
  • Also, note that StackOverflowError is a runtime exception. It's impossible to detect at compile time. – jonvuri Oct 07 '12 at 15:50
  • @Kiyura related to the halting problem? – Jakob Bowyer Oct 07 '12 at 15:50
  • @Kiyura: exactly. But when he runs the code above, he should see this error, given that his fact int parameters actually increase on recursion. – Hovercraft Full Of Eels Oct 07 '12 at 15:50
  • @HovercraftFullOfEels, yup, I was just addressing his apparent concern that he couldn't see this problem before running it. – jonvuri Oct 07 '12 at 15:51
  • @HovercraftFullOfEels : Why the StackOverflowError not caught by either the compiler of the exception, so how to handle that error in future. so why the infinite loop occurs. – Rand Mate Oct 07 '12 at 15:59
  • 1
    Because, compiler will not know what integer you are about to pass . You can get the input from the user at runtime and pass it to the fact method. Thats the reason this kind of error/exceptions are not handled at compile time.On a side not, any error or exception that extends RunTimeException will not be handled by compiler. Have a look at : http://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html – Jimmy Oct 07 '12 at 16:12
  • @HovercraftFullOfEels: sorry since the loop exceeded with infinite error log I cannot able to see the stack overflow error but now i saw it. – Rand Mate Oct 07 '12 at 16:19

3 Answers3

3

You have a mistake in the return value expression of fact method. It should be

      return fact(m-1) * m;
  • while say for example i amd using return(a+b), it executes successfully whats the problem with the recursion to show an error??? – Rand Mate Oct 07 '12 at 15:54
0
      return (fact ((m-1)*m));

returns

fact(20)

which returns

fact (380)

which returns

fact (379*380)

which ....

which never returns anything and should make a stack overflow (too much memory is used on the call stack).

           return fact(m-1) * m;

should work.

I highly recommend you reading again the basics, and examples ( here, for example - http://www.toves.org/books/java/ch18-recurex/index.html)

Try writing the recursion tree yoursels, in order to understand what happens.

user1249569
  • 83
  • 1
  • 3
  • 10
0

Another way to calculate factorial using cycle (with no recursion):

int fact(int m) throws Exception
{
    int f = 1;
    for (int i = 0; i < m; f *= ++i);
    return f;
}
Usman Salihin
  • 291
  • 1
  • 8