`
lige239141
  • 浏览: 171573 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

finally和return

阅读更多
package demo1;
/**
 * (1)首先执行finally,然后才执行return;
   (2)如果finally里面有return,try中的return不会再执行;
   (3)如果try中有了异常,return也不执行;
 */
public class Test1 {
	public static int f1(){
		int i=1;
		try{
			i=2;
			return i;
		}catch(Exception e){
			return i;
		}finally{
			i=3;
			System.out.println("finally");
		}
	}
	public static int f2(){
		int i=1;
		try{
			i=2;
			return i;
		}catch(Exception e){
			return i;
		}finally{
			i=3;
			System.out.println("finally");
			return i;
		}
	}
	public static int f3(){
		int i=1;
		try{
			i=i/0;
			return i;
		}catch(Exception e){
			return i;
		}finally{
			i=3;
			System.out.println("finally");
		}
	}
	
	public static void main(String[] args){
		System.out.println(f1());
		System.out.println(f2());
		System.out.println(f3());
	}
} 

 运行结果:

 

 

finally
2
finally
3
finally
1
 

 

 

分享到:
评论
1 楼 GaoMatrix 2011-02-10  
你写的这个不对呀,应该是先return,然后执行finally,你的运行结果也是说明了这一点呀

相关推荐

Global site tag (gtag.js) - Google Analytics