`
trydofor
  • 浏览: 144936 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

必须知道,别滥用-finally

 
阅读更多
public class Test {
    
    public static int testInt() {
        int x = 0;
        try {
            x = 1;
            System.out.println("x=1");
            return x;
        }
        finally {
            x = 2;
            System.out.println("x=2");
        }
    }
    
    public static Ref testRef() {
        Ref ref = new Ref();
        try {
            ref.x = 1;
            System.out.println("x=1");
            return ref;
        }
        finally {
            ref.x = 2;
            System.out.println("x=2");
        }
    }
    
    public static void main(String[] args) {
        System.out.println("x=? " + testInt());
        System.out.println("x=? " + testRef());
    }
    
    private static class Ref {
        private int x;
        public String toString(){
            return String.valueOf(x);
        }
    }
}
分享到:
评论
2 楼 trydofor 2011-04-22  
参考资料
http://supermmx.org/blog/20061109_try_catch_finally
1 楼 trydofor 2011-04-22  
后续跟踪:
使用javap分析finally块中return值
http://blog.csdn.net/aspire_sun/archive/2011/04/22/6340532.aspx

相关推荐

Global site tag (gtag.js) - Google Analytics