论坛首页 Java企业应用论坛

try catch finally的递归调用

浏览 32479 次
精华帖 (10) :: 良好帖 (0) :: 新手帖 (5) :: 隐藏帖 (1)
作者 正文
   发表时间:2011-07-19  
private static void foo() {   
	    try {   
	        System.out.println("try");   
	        foo();   
	    } catch (Throwable e) {   
	        System.out.println("catch");   
	        foo();   
	    } finally {   
	        System.out.println("finally");   
	        foo();   
	    }   
	}   
	  
	public static void main(String[] args) {   
	    foo();   
	}  

想了半天还是不明白到底输出是什么形式的,希望大神门都能告诉小弟一声!
   发表时间:2011-07-19  
try2863
try2864
try2865
try2866
try2867
try2868
try2869
try2870
try2871
try2872
try2873
try2874
try2875
try2876
try2877
try2878
try2879
try2880
try2881
try2882
try2883
try2884
try2885
try2886
try2887
try2888
try2889
try2890
try2891
try2892
try2893
try2894
try2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897finally2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2894
try2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897finally2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897finally2894
try2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897finally2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2893
try2894
try2895
try2896
try2897catch2897finally2897catch2896
try2897catch2897finally2897finally2896
try2897catch2897finally2897catch2895
try2896


我机器上的执行,我也纳闷了,单线程的跑,貌似又是finally也没执行,而是执行了几次try?实际上每个i的值try和finally都打印出来了,我传递的是同一个i值,只在方法开始前+.
我的版本1.6.0_10
0 请登录后投票
   发表时间:2011-07-19  
自己拿到自己的环境中运行一下呗。。。。。
0 请登录后投票
   发表时间:2011-07-19  
理论上是无限递归,结果是只会输出 try
0 请登录后投票
   发表时间:2011-07-19  
ptma 写道
理论上是无限递归,结果是只会输出 try

然而实际执行过程中,肯定会由于堆栈溢出等问题出现异常,从而出现catch和finally。
具体的溢出点,则要看具体的堆栈大小和虚拟机实现的情况了。
0 请登录后投票
   发表时间:2011-07-19  
abettor 写道
ptma 写道
理论上是无限递归,结果是只会输出 try

然而实际执行过程中,肯定会由于堆栈溢出等问题出现异常,从而出现catch和finally。
具体的溢出点,则要看具体的堆栈大小和虚拟机实现的情况了。

如果栈溢出就会抛异常,那么catch也会输出,就是没有啊?
0 请登录后投票
   发表时间:2011-07-19  
蛋疼的问题,有意义?
0 请登录后投票
   发表时间:2011-07-19  
你在浪费自己的时间,也在浪费我们的时间
0 请登录后投票
   发表时间:2011-07-19  
我觉得这个问题挺好的, 有助于理解JVM 和异常捕获的运行机制。
0 请登录后投票
   发表时间:2011-07-19  
Laosong 写道
abettor 写道
ptma 写道
理论上是无限递归,结果是只会输出 try

然而实际执行过程中,肯定会由于堆栈溢出等问题出现异常,从而出现catch和finally。
具体的溢出点,则要看具体的堆栈大小和虚拟机实现的情况了。

如果栈溢出就会抛异常,那么catch也会输出,就是没有啊?



StackOverflowError是错误,程序捕获不到!
1 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics