unpredicted result

class crash

{

static int i=1;

public static void main(String []args)throws Exception

{

String[] a={"any","string"};

try{

main(a);

System.out.println("i="+(i++));

}

catch(Error e){

System.out.println("Error"+e);

}

finally{

System.out.println("Finally......");

}

}//end of main

}//end of class

After running this I got unpredicted result.

Here I am using recursion.I expected JVM recusively call main() method without displaying any output and at the end it display Erroe messager(because calling main() method is the first statement here)

[658 byte] By [rajesh_borusua] at [2007-10-3 4:53:07]
# 1

One call to main(a) will generate StackOverflowError, so the catch block will be executed, and the method will terminate normally (but recursion is stopped as call to main failed), and the program will continue.

The only thing that's difficult to predict (maybe) is the number of recursive calls before stack overflow (I guess it depends on VM parameters.)

The behaviour you expect (i.e. crash, as far as I understand you) happens if you don't catch the Error.

TimTheEnchantora at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 2
what result do you get? and how is unpredictable
kilyasa at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 3
I am getting Errorjava.lang.StackOverflowError and I totally expected that
kilyasa at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 4

I got the below result

Output:

i=1

Finally......

i=2

Finally......

:

:

:

i=4747

Finally......

i=4748

Finally......

This the output.

I have expected JVM will display Error Object.Because calling the main() method is the first statement before the

System.out.println("i="+(i++));.So I expect JVM recursively call main() with out displaying any i value. And every time JVM excute finelly block,why?

rajesh_borusua at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 5
> I got the below resultReally ? It should start like this :Errorjava.lang.StackOverflowErrorFinally......i=0Finally......i=1Finally......etc.
TimTheEnchantora at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 6
What happens is that you call the main method, and that will start a new thread in the JVM and when you start the 4748:th thread, you will get StackOverflowError and then the rest of the treads will exit normally.
Lajma at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> What happens is that you call the main method, and

> that will start a new thread in the JVM and when you

> start the 4748:th thread, you will get

> StackOverflowError and then the rest of the treads

> will exit normally.

Total bull. There are no threads being created here, except for the one implicit one that was initially created for the VM before it initially invoked main.

There is no magic in the method name. It does not magically create new threads just because it is invoked internally.

warnerjaa at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 8

> And every time JVM excute finelly block,why?

The finally clause is guaranteed to execute, even after an exception is thrown or after returning a value.

private void doBtnTest() {

int i = testInt();

System.out.println("i is: "+i);

}

private int testInt() {

try {

return 1;

} finally {

System.out.println("Finally");

}

}

The output is:

Finally

i is: 1

BinaryDigita at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...
# 9
> What happens is that you call the main method, and> that will start a new thread Nope. This code does not start any new threads.
jverda at 2007-7-14 22:57:57 > top of Java-index,Java Essentials,Java Programming...