当前位置: 代码迷 >> Java相关 >> 诡异的finally
  详细解决方案

诡异的finally

热度:288   发布时间:2010-07-16 13:34:08.0
诡异的finally
碰到这样一个程序:package com.whpu.test.w716;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FinallyDemo {

    public String getValue(){
        String msg = "ok";
        try {
            FileInputStream fin = new FileInputStream("d:/a.txt");//有a.txt此此文件
            fin.read();
            return msg;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            msg = "finally";
            System.out.println("我被调用");
        }
        
        return msg;
    }
   
   
    public static void main(String[] args) {
        FinallyDemo f = new FinallyDemo();
        System.out.println(f.getValue());
    }

}
输出结果为:
我被调用
ok
//本人不解,请大虾解释下
搜索更多相关的解决方案: finally  

----------------解决方案--------------------------------------------------------
如果finally语句中没有return语句覆盖的话,那么先执行try 的return 后执行finally
----------------解决方案--------------------------------------------------------
回复 2楼 lampeter123
能不能说的详细的呢?
----------------解决方案--------------------------------------------------------
finally块总是会被执行的!!!
----------------解决方案--------------------------------------------------------
那么msg的值就应该是  finally啊,可是打印出来的结果是  ok
----------------解决方案--------------------------------------------------------
只有return 才会返回值啊!
----------------解决方案--------------------------------------------------------
程序执行到try中的return时,虽然没有执行,但是已经执行了return后面的msg,也就是说,不管finally中再对msg做什么变化,只要finally执行完了再回来执行try的return,返回的msg是执行finally之前的值,如果把return后面的msg改为方法,看着就更直观了,如下:
程序代码:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FinallyDemo {
    static String msg = "ok";
    public static String one(){
        System.out.println("one");
        return msg+" "+"one";
    }
    public String getValue(){
      
        try {
            FileInputStream fin = new FileInputStream("d:/a.txt");//有a.txt此此文件
            fin.read();
            return one();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            msg = "finally";
            System.out.println("我被调用");
        }
     
        return msg;
    }


    public static void main(String[] args) {
        FinallyDemo f = new FinallyDemo();
        System.out.println(f.getValue());
    }
}
输出结果是:
one
我被调用
ok one
//程序执行到try的return的时候虽然没有执行,但是return后面的方法(你程序里是msg字符串)却执行了,后面再对它做修改也不会影响它的值

----------------解决方案--------------------------------------------------------
如果return msg;在finally里面,就会返回msg="finally", 复盖之前的try return
----------------解决方案--------------------------------------------------------
finally块总是会被执行的,不管程序抛出异常还是正常运行,finally一定能执行。所以在调用getValue()的时候,
返回了ok,并且执行System.out.println("我被调用");所以输出 我被调用。程序中又把返回结果打印出来
System.out.println(f.getValue());
所以再输出ok了。

----------------解决方案--------------------------------------------------------
回复 7楼 syg5434
程序代码:
public class FinallyDemo {
    static String msg = "ok";

    public static String one() {
        System.out.println("one");
        return msg + " " + "one";
    }

    public String getValue() {

        try {
            // FileInputStream fin = new
            
// FileInputStream("d:/a.txt");//有a.txt此此文件
            
// fin.read();
            return one();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            msg = "finally";
            System.out.println("我被调用");
        }

        return msg;
    }

    public static void main(String[] args) {
        FinallyDemo f = new FinallyDemo();
        System.out.println(f.getValue());
        System.out.println(f.msg);
    }
}

你输出getVaule();return one();当时finally还没被执行,msg = "ok";
再写个输出语句System.out.println(f.msg);
你就可以清楚的看到,msg被改变了
----------------解决方案--------------------------------------------------------
  相关解决方案