aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling Main method Recursively. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling Main method Recursively." Watch "Calling Main method Recursively." New topic
Author

Calling Main method Recursively.

karthik hv
Greenhorn

Joined: Jun 11, 2006
Posts: 5
When I use the below mentioned code, the main is called recursively.
But it is called only 9112 times. Why not Infinite times.
Then the program terminates abnormally.
Even the exception is not caught.

Can any one explain me why this is happening.If so, why only 9112 times.

public class CallMain
{
static int count;
public static void main(String args[])
{
try
{
System.out.println("Main Called"+ ++count);
String anotherArgs[] = {"K","P"};
main(anotherArgs);
}
catch(Exception e)
{
System.out.println("Exception is:"+e);
}
}
}
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
Each thread will have its own stack. For every method call it will put the entry in the stack and after the execution of method the entry will be popped.
So , once the stack entries exceeds the size of stack you will get StackOverflowError.


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
karthik hv
Greenhorn

Joined: Jun 11, 2006
Posts: 5
But is "StackOverflowError" not an Exception to be caught?
If so, Why didnt I catch it in Generic Exception. "Exception e"

Regards,
Karthik
Rajasekar Elango
Ranch Hand

Joined: Sep 13, 2004
Posts: 105
If you want to catch "StackOverflowError" , you have to catch "StackOverflowError" "Error" or "Throwable".
"Throwable" is the root of exception hierarchy and "Error" and "Exception" are subclasses of "Throwable" and "Exception" cannot catch "Error"

Regards,
E. Rajasekar.


SCJP 1.4
Ashish Laddha
Greenhorn

Joined: Apr 12, 2005
Posts: 21
Just putting the code

class CallMain
{
static int count;
public static void main(String args[])
{
try
{
System.out.println("Main Called"+ ++count);
String anotherArgs[] = {"K","P"};
main(anotherArgs);
}
catch(Throwable e)
{
System.out.println("Throwable is:"+e);
}
}
}

And I dont think 9112 has some signifiacance. For me it is some other number. It is just that when the stack gets overflown, the error is thrown.
Now, let me ask another question

"How to increase the size of the stack?"
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
java -Xss1024k <classname>

Here the stack size is 1024 Kb.
Banu Ravi
Greenhorn

Joined: Jun 12, 2006
Posts: 14
Hi Karthik

I tried in eclipse 3.1;
i copied the source code & executed.

Got till Main Called3620
java.lang.StackOverflowError
Exception in thread "main"
[ June 15, 2006: Message edited by: Banu Ravi ]
karthik hv
Greenhorn

Joined: Jun 11, 2006
Posts: 5
Hi,

Thank you Balasubramani, Rajasekar, Ashish, Banu , for providing the solution.

Balasubramani:

i tried to execute the command you had provided,

java -Xss1024k <classname>

With mine class name as,

java -Xss100000k CallMain
and
java -Xss1k CallMain

But for both , I get this as output.
It ends with

...
...
Main Called9110
Main Called9111
Main Called9112

The stack size is not changed.
Please tell me whether I am missing out something.

P.S. : I executed this in jdk1.3
 
wood burning stoves
 
subject: Calling Main method Recursively.