zoukankan      html  css  js  c++  java
  • Java中的finally 和return

    直接上代码

    public static int a(){
            int i = 1;
            try{
                i++;
                return ++i;
            }catch(Exception e){
    
            }finally {
                if(i > 1){
                    System.out.println(" i =  " + i);// i =  3
                }
            }
            return 0;
        }
    
        public static void main(String[] args) {
            System.out.println(a());//  结果: 3
        }

    finally 中的代码必然会执行,但是是在执行return的逻辑后 ,方法返回之前执行的。

    下面再看一段代码:

    public static int a(){
            int i = 1;
            try{
                i++;
                return ++i;
            }catch(Exception e){
    
            }finally {
                if(i > 1){
                    System.out.println(" i =  " + i);// i =  3
                }
                return ++i;
            }
    
        }
    
        public static void main(String[] args) {
            System.out.println(a());// 4
        }

    此时,方法返回的结果是4,也就是说,返回值被覆盖了。

  • 相关阅读:
    c++常用库
    boost
    android
    UITableView 多选
    c++ 比较两个集合
    事件加不上的另一种原因
    ios多线程
    ubuntu android
    jna StdCallCallback 回调问题查证
    java
  • 原文地址:https://www.cnblogs.com/yrjns/p/13540750.html
Copyright © 2011-2022 走看看