java面试之finally与return

本文通过多个示例详细解析了Java中finally块与return语句的交互作用,展示了不同情况下程序如何执行,以及finally块中return的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.若finally前面有return,怎么样?

答: finally会在return前执行

例题一:

public  classTest {

    public static void main(String[] args) {
       System.out.println(newTest().test());
    }
    staticint test()
    {
       int x = 1;
       try
       {
           return x;
       }
       finally
       {
           ++x;
       }
    }
}

返回1,说明是x先放到返回的填充的地方,然后再在finally加一,但对返回无影响,然后return 返回;

例题二:返回:2 

public  classTest {

    public static void main(String[] args) {
       System.out.println(newTest().test());
    }
    staticint test()
    {
       int x = 1;
       try
       {
           return ++x;
       }
       finally
       {
           ++x;
       }
    }


例题三:返回:1


public class MyTest {
static byte[] buffer = new byte[1024];
public static void main(String[] args) {
int n = -1;
try {
n = getString();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("result=" + n);
}

public static int getString() throws MyException {
FileInputStream fi = null;
int n = 0;
try {
boolean b = false;
fi = new FileInputStream(new File("./2.txt"));
n = fi.read(buffer);
n = 3;
return n;
} catch (FileNotFoundException e) {
e.printStackTrace();
return ++n;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
n++;
if (fi != null) {
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return n;
}

}

例题四:报错,且返回:-1,

java.lang.NullPointerException
at com.jimmy.MyTest.getString(MyTest.java:58)
at com.jimmy.MyTest.main(MyTest.java:20)
result=-1

public class MyTest {
static byte[] buffer = new byte[1024];
public static void main(String[] args) {
int n = -1;
try {
n = getString();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("result=" + n);
}

public static int getString() throws MyException {
FileInputStream fi = null;
int n = 0;
try {
boolean b = false;
//fi = new FileInputStream(new File("./2.txt"));注意这里注销了,fi为空
n = fi.read(buffer);
n = 3;
return n;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return ++n;
} catch (IOException e) {
e.printStackTrace();
} finally {
n++;
try {
fi.close(); // 同样fi为空,但是这里没有NullPointerException的处理,所以往上抛到了,被getString()的异常处理捕获
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return n; //这里没执行
}
}
例题五(reference:http://blog.youkuaiyun.com/hguisu/article/details/6155636):返回:

i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false


public class MyExceptionTest {  
    public MyExceptionTest() {  
    }  
  
    boolean testEx() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx1();  
        } catch (Exception e) {  
            System.out.println("testEx, catch exception");   //没有异常被抛出,这里不执行
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    boolean testEx1() throws Exception {  
        boolean ret = true;  
        try {  
            ret = testEx2();  
            if (!ret) {  
                return false;  
            }  
            System.out.println("testEx1, at the end of try");  
            return ret;  
        } catch (Exception e) {  
            System.out.println("testEx1, catch exception");   //没有异常被抛出,这里不执行
            ret = false;  
            throw e;  
        } finally {  
            System.out.println("testEx1, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    boolean testEx2() throws Exception {  
        boolean ret = true;  
        try {  
            int b = 12;  
            int c;  
            for (int i = 2; i >= -2; i--) {  
                c = b / i;  
                System.out.println("i=" + i);  
            }  
            return true;  
        } catch (Exception e) {  
            System.out.println("testEx2, catch exception");  
            ret = false;  
            throw e;    //这里相当于return,可是return前要运行finally ,可在finally里有return ,就提前返回了
        } finally {  
            System.out.println("testEx2, finally; return value=" + ret);  
            return ret;  
        }  
    }  
  
    public static void main(String[] args) {  
    MyExceptionTest testException1 = new MyExceptionTest();  
        try {  
            testException1.testEx();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值