当前位置: 首页 > news >正文

finally执行语句的注意和小陷阱

public class FinallyTest {
    public static void main(String[] args) {
            int res=div(10,0);
        System.out.println(res);
    }
    public static int div(int a,int b){
        int num=0;
        try {
            num=a/b;
        }catch (ArithmeticException e){
            System.out.println("算数异常");
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("其他异常");
        }finally {
            System.out.println("一定执行的语句");
        }
        return num;
    }
}

public class FinallyTest {
    public static void main(String[] args) {
            int res=div(10,0);
        System.out.println(res);
    }
    public static int div(int a,int b){
        int num=0;
        try {
            num=a/b;
        }catch (ArithmeticException e){
            System.out.println("算数异常");
            return 11;
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("其他异常");
        }finally {
            System.out.println("一定执行的语句");
        }
        return num;
    }
}

 

结论:

finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行

/*
finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
 */
public class FinallyTest {
    public static void main(String[] args) {
            int res=div(10,0);
        System.out.println(res);
    }
    public static int div(int a,int b){
        int num=0;
        try {
            num=a/b;
        }catch (ArithmeticException e){
            System.out.println("算数异常");
            return 11;
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("其他异常");
        }finally {
            System.out.println("一定执行的语句");
            return 22;
        }
    }
}

 

/*
finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
 */
public class FinallyTest {
    public static void main(String[] args) {
            int res=div(10,0);
        System.out.println(res);
    }
    public static int div(int a,int b){
        int num=0;
        try {
            num=a/b;
        }catch (ArithmeticException e){
            System.out.println("算数异常");
            return num;
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("其他异常");
        }finally {
            num+=100;
            System.out.println("一定执行的语句");
        }
        return num;
    }
}

/*
finally 是可选的,一旦写上就一定执行的语句,即使过程中有return也一定执行
 */
public class FinallyTest {
    public static void main(String[] args) {
            int res=div(10,0);
        System.out.println(res);
    }
    public static int div(int a,int b){
        int num=0;
        try {
            num=a/b;
        }catch (ArithmeticException e){
            System.out.println("算数异常");
            return num;
        }catch (NumberFormatException e){
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("其他异常");
        }finally {
            num+=100;
            System.out.println("一定执行的语句");
            return num;
        }
    }
}

相关文章:

  • 【推荐系统->论文阅读】WideDeep模型
  • 【Node】cookie、sessionStorage、localStorage 与 身份认证
  • 把setting.xml放在conf和.m2目录的区别
  • OpenCV图像加载、显示与保存
  • Vulhub靶场搭建与使用
  • 80-Java的Map集合:概述、API、遍历方式
  • vue中什么是$nextTick?
  • java springboot儿童医药评价系统网站python
  • 12.springboot中使用自定义Filter
  • 【JS缓存技术】-本地存储
  • 目标检测——关键点检测学习记录(五):物体关键点检测
  • Android开发学习——3.平台版本、SDK版本、API级别
  • python数据分析(2)
  • tsx 文件中的 行删除
  • Visual C++基础 - 使用OLE/COM操作Excel类
  • 【mysql】环境安装、服务启动、密码设置
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • CSS实用技巧
  • Hibernate最全面试题
  • Joomla 2.x, 3.x useful code cheatsheet
  • js算法-归并排序(merge_sort)
  • mysql 5.6 原生Online DDL解析
  • Python学习之路13-记分
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • Vue组件定义
  • windows下如何用phpstorm同步测试服务器
  • 动手做个聊天室,前端工程师百无聊赖的人生
  • 排序算法之--选择排序
  • 让你的分享飞起来——极光推出社会化分享组件
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 微信支付JSAPI,实测!终极方案
  • 写代码的正确姿势
  • 怎样选择前端框架
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • 从如何停掉 Promise 链说起
  • 你学不懂C语言,是因为不懂编写C程序的7个步骤 ...
  • ​【已解决】npm install​卡主不动的情况
  • #pragma multi_compile #pragma shader_feature
  • (Arcgis)Python编程批量将HDF5文件转换为TIFF格式并应用地理转换和投影信息
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (PWM呼吸灯)合泰开发板HT66F2390-----点灯大师
  • (zz)子曾经曰过:先有司,赦小过,举贤才
  • (层次遍历)104. 二叉树的最大深度
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (附源码)ssm教材管理系统 毕业设计 011229
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • .describe() python_Python-Win32com-Excel
  • .net core开源商城系统源码,支持可视化布局小程序
  • .Net mvc总结
  • .NET连接数据库方式
  • .NET企业级应用架构设计系列之技术选型
  • .Net语言中的StringBuilder:入门到精通
  • [acm算法学习] 后缀数组SA
  • [BZOJ 3680]吊打XXX(模拟退火)