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)
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.
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?
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 >

> 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.
> 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