栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

finally return时详解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

finally return时详解

finally return时详解

具体如下代码:

package com.example.demo.java;

import java.util.ArrayList;
import java.util.List;


public class FinallyTest {
    private static int  a =10;
    private static List intList = new ArrayList();

    public static void main(String[] args) {
        System.out.println("finallyTest1:" +finallyTest1());//执行结果返回13
        System.out.println("finallyTest2:" +finallyTest2());//执行结果返回12
        System.out.println("finallyTest3:" +finallyTest3());//执行结果返回11
        System.out.println("finallyTest4:" +finallyTest4().get(0));//执行结果返回20

    }
    
    public static int finallyTest1(){
        try{
            a = 11;
            throw new Exception();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            a=13;
            return a;
        }

    }
    
    public static int finallyTest2(){
        try{
            a = 11;
            return a;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            return a =12;
        }
    }

    
    public static int finallyTest3(){
        try{
            a = 11;
            return a;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            a =14;
        }
        return a;
    }

    
    public static List finallyTest4(){
        try{
            a = 11;
            intList.add(a);
            return intList;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            intList.set(0,20);
        }
        return intList;
    }
}

结果如截图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/353975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号