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

【Java】finally用法

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

【Java】finally用法

文章目录
  • 一.概述
  • 二. finally会执行的情况
    • 1.有catch(无异常)
    • 3.有catch(try异常)
    • 4.有catch(catch异常)
    • 5.有catch(try/catch都异常)(会抛出异常)
    • 6. 没有catch(无异常)
    • 7. 没有catch(try异常)(会抛出异常)
    • 8. 有返回值(try)(程序返回"try return")
    • 9. 有返回值(catch)(程序返回"catch return")
  • 三. finally不会执行的情况
    • 1. 调用 System.exit 函数
    • 2. 调用 halt 函数
  • 四. 常见问题
    • 1. 忽略异常(程序返回"finally return")
    • 2. finally存在return语句,则 try 和 catch 存在的返回语句就会被忽略(程序返回"finally return")
    • 3. finally抛异常(不会有返回值,一直抛出异常 RuntimeException)
    • 4. finally异常覆盖try异常
    • 5. finally异常覆盖catch异常
    • 6. finally异常覆盖其它异常原因及解决

一.概述
  1. 本文说明Java中finally的用法和可能遇到的坑
  2. finally的目的是保证代码被执行,但也会存在不执行的情况
  3. finally 代码块的原理是复制 finally 代码块的内容,分别放在 try-catch 代码块所有正常执行路径以及异常执行路径的出口中。
    所以不管是是正常还是异常执行,finally都是最后执行的。
二. finally会执行的情况 1.有catch(无异常)
try {
    System.out.println("try execute");
    
} catch (RuntimeException e) {
    System.out.println("catch execute");
    
} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute
3.有catch(try异常)
try {
    System.out.println("try execute");
    throw new RuntimeException("try Exception");

} catch (Exception e) {
    System.out.println("catch execute");

} finally {
    System.out.println("finally execute");
}
输出
try execute
catch execute
finally execute
4.有catch(catch异常)
try {
    System.out.println("try execute");

} catch (Exception e) {
    System.out.println("catch execute");
    throw new RuntimeException("catch Exception");

} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute
5.有catch(try/catch都异常)(会抛出异常)
 try {
    System.out.println("try execute");
    throw new RuntimeException("try Exception");

} catch (Exception e) {
    System.out.println("catch execute");
    throw new RuntimeException("catch Exception");

} finally {
    System.out.println("finally execute");
}
输出
try execute
catch execute
finally execute
Exception in thread "main" RuntimeException: catch Exception

6. 没有catch(无异常)
try {
    System.out.println("try execute");
} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute
7. 没有catch(try异常)(会抛出异常)
try {
    System.out.println("try execute");
    throw new RuntimeException("try Exception");

} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute
Exception in thread "main" ServiceException: try Exception
8. 有返回值(try)(程序返回"try return")
try {
    System.out.println("try execute");
    return "try return";

} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute
9. 有返回值(catch)(程序返回"catch return")
try {
    System.out.println("try execute");
    throw new RuntimeException("try exception");

} catch (Exception ex) {
    return "catch return";
} finally {
    System.out.println("finally execute");
}
输出
try execute
finally execute

三. finally不会执行的情况 1. 调用 System.exit 函数
try {
    System.out.println("try execute");
    System.exit(1);

} catch (Exception ex) {

    System.out.println("catch execute");

} finally {

    System.out.println("finally execute");
}
输出
try execute

2. 调用 halt 函数
try {
    System.out.println("try execute");
    Runtime.getRuntime().halt(1);

} catch (Exception ex) {

    System.out.println("catch execute");

} finally {

    System.out.println("finally execute");
}
输出
try execute

四. 常见问题 1. 忽略异常(程序返回"finally return")
try {
    System.out.println("try execute");
    throw new RuntimeException("try exception");

} finally {

    System.out.println("finally execute");
    return "finally return";
}
输出
try execute
finally execute

2. finally存在return语句,则 try 和 catch 存在的返回语句就会被忽略(程序返回"finally return")
try {
    System.out.println("try execute");
    return "try return";

} finally {
    System.out.println("finally execute");
    return "finally return";
}
输出
try execute
finally execute

3. finally抛异常(不会有返回值,一直抛出异常 RuntimeException)
try {
    System.out.println("try execute");
    return "try return";

} finally {

    System.out.println("finally execute");
    throw new RuntimeException("finally exception");
}
输出
try execute
finally execute
Exception in thread "main" java.lang.RuntimeException: finally exception

4. finally异常覆盖try异常
try {
    System.out.println("try execute");
    throw new RuntimeException("try exception");

} finally {

    System.out.println("finally execute");
    throw new RuntimeException("finally exception");
}
输出
try execute
finally execute
Exception in thread "main" java.lang.RuntimeException: finally exception

5. finally异常覆盖catch异常
try {
    System.out.println("try execute");
    throw new RuntimeException("try exception");

} catch (Exception ex) {

    System.out.println("catch execute");
    throw new RuntimeException("catch exception");

} finally {

    System.out.println("finally execute");
    throw new RuntimeException("finally exception");
}
输出
Exception in thread "main" java.lang.RuntimeException: finally exception
try execute
catch execute
finally execute

6. finally异常覆盖其它异常原因及解决

原因:一个方法只能抛出一种异常,无法出现两种

解决1:finally代码块自行捕获和处理异常

try {
    System.out.println("try execute");

} finally {

    System.out.println("finally execute");

    try {

        throw new RuntimeException("finally exception");

    } catch (Exception ex) {

        log.error(ex.getMessage());
    }
}
输出
try execute
finally execute
错误日志:finally exception

解决2:异常追加

Exception e = null;

try {
    System.out.println("try execute");
    throw new RuntimeException("try exception");

} catch (Exception ex) {
    System.out.println("catch execute");
    e = ex;

} finally {
    System.out.println("finally execute");
    try {

        throw new RuntimeException("finally exception");

    } catch (Exception ex) {

        if (e != null) {
            e.addSuppressed(ex);
        } else {
            e = ex;
        }
    }
}

throw e;
输出
try execute
catch execute
finally execute
Exception in thread "main" java.lang.RuntimeException: try exception
    Suppressed: java.lang.RuntimeException: finally exception
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/434637.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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