Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Main method Recursively.

 
karthik hv
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
karthik hv
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ashish Laddha
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java -Xss1024k <classname>

Here the stack size is 1024 Kb.
 
Banu Ravi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic