Finally的使用场景解析

转载出处 :
Java finally 的用法,看这一篇就够了

概述

通常finally和try同用,下面会介绍finally的一般用法、什么时候回执行finally,什么时候不会执行finally,什么时候有返回值,什么时候没有返回值。通过一些例子来和大家共同分享一下。

什么是finally

一般try关键字之后可以定义finally代码块,这块代码通常会在try语句和catch语句之后,方法结束之前执行。
通常无论有没有异常finally语句都会执行。

finally用法

1、简单的finally示例

public class FinallyDemo {


    private void finallyTest(){

        try {

            System.out.println("try 代码块!");
            throw  new Exception();

        } catch (Exception e) {

            System.out.println("catch 代码块!");

        } finally {

            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }


}
输出结果:
try 代码块!
catch 代码块!
finally 代码块!

Process finished with exit code 0
总结:
     通常finally语句会在try和catch之后正常执行

2. 不带catch的finally语句

public class FinallyDemo {
    
    private void finallyTest(){
       
        try {
            System.out.println("try 代码块!");

        } finally {

            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }
    
}
输出结果:
try 代码块!
finally 代码块!
总结:
   有没有异常处理器catch对finally执行没有影响

3. finally使用场景

finally代码块通常都会执行,一般可以在finally代码块中执行一些数据库关闭、文件流关闭、释放线程资源等用途场景

finally的执行机制

1. 没有异常

public class FinallyDemo {

    private void finallyTest(){

        try {
            System.out.println("try 代码块!");

        } finally {

            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }

}
输出结果
try 代码块!
finally 代码块!

2、有异常无异常处理器

// jdk低版本可以正常执行,高版本没有catch语句会报错
try {
    System.out.println("Inside try");
    throw new Exception();
} finally {
    System.out.println("Inside finally");
}

以下是jdk8版本会直接报错
在这里插入图片描述

总结
    高版本对于有异常无异常处理器的代码块会报错

3.有异常处理器

public class FinallyDemo {

    private void finallyTest(){

        try {
            System.out.println("try 代码块!");
            throw new Exception();
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {

            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }

}
输出结果:
try 代码块!
catch 代码块!
finally 代码块!

4. try代码块有返回值

public class FinallyDemo {

    private String finallyTest(){

        try {
            System.out.println("try 代码块!");
            return "try!";
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {
            System.out.println("finally 代码块!");
        }
        return "end";
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }

}
输出结果:
try 代码块!
finally 代码块!
总结:
    try中有返回值不会影响finally的正常执行

5. catch中有返回值

public class FinallyDemo {

    private String finallyTest(){

        try {
            System.out.println("try 代码块!");
            throw new Exception();
        } catch (Exception e) {
            System.out.println("catch 代码块!");
            return "try!";
        } finally {
            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }

}
返回结果:
try 代码块!
catch 代码块!
finally 代码块!
总结:
    catch中的返回值也不会影响finally的正常执行

finally不执行的场景

虽然通常情况下编写finally语句块是为了让这部分代码一定执行,但某些情况下finally语句是不会被执行的
比如程序被中断,那么finally肯定不会被执行,类似的其它场景也是如此

1.调用System.exit()函数

public class FinallyDemo {

    private void finallyTest(){

        try {
            System.out.println("try 代码块!");
            System.exit(0);
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {
            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }
}
执行结果:
try 代码块!

调用halt 函数

public class FinallyDemo {

    private void finallyTest(){

        try {
            System.out.println("try 代码块!");
            Runtime.getRuntime().halt(0);
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {
            System.out.println("finally 代码块!");
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        demo.finallyTest();
    }
}
输出结果:
try 代码块!

Process finished with exit code 0
总结:
    程序退出都会导致finally语句不被执行
    System.exit()函数会等线程执行完成后退出程序,会做一些资源回收等后续动作。
    halt函数则只会立刻退出程序,不会等待线程执行完成,也不会做资源回收等后续处理动作

3.守护线程

import java.util.concurrent.TimeUnit;
/**

 * Finally shoud be always run ?

 */
public class DaemonsDontRunFinally {

    public static void main(String[] args) {

        Thread t = new Thread(new ADaemon());

        t.setDaemon(true);

        t.start();

    }
}

class ADaemon implements Runnable {

    public void run() {

        try {

            System.out.println("start ADaemon...");

            TimeUnit.SECONDS.sleep(1);

        } catch (InterruptedException e) {

            System.out.println("Exiting via InterruptedException");

        } finally {

            System.out.println("This shoud be always run ?");

        }

    }

}
总结:
    如果守护线程刚开始执行到 finally 代码块,此时没有任何其他非守护线程,那么虚拟机将退出,此时 JVM 不会等待守护线程的 finally 代码块执行完成。

4. try无线循环

try {
    System.out.println("Inside try");
    while (true) {
    }
} finally {
    System.out.println("Inside finally");
}

总结:
    Try 代码块出现无限循环,且不出现异常,finally 也将永远得不到执行

场景陷阱

1、 如果finally语句中有返回值则,则会忽略try中的返回值
public class FinallyDemo {

    private String finallyTest(){

        try {
            System.out.println("try 代码块!");
            return "try";
            //throw new Exception();
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {
            System.out.println("finally 代码块!");
            return "end";
        }
    }

    public static void main(String[] args) {

        FinallyDemo demo = new FinallyDemo();
        String ret = demo.finallyTest();
        System.out.println(ret);

    }
}
输出结果:
try 代码块!
finally 代码块!
end
总结:
    无论是否有异常处理器,finally中的return一定会被执行,而且会覆盖try中的return值

2、如果finally中有exception抛出,无论try中返回任何值,都只会返回finally中的异常值

public class FinallyDemo {

    private String finallyTest() throws Exception {

        try {
            System.out.println("try 代码块!");
            return "try";
        } catch (Exception e) {
            System.out.println("catch 代码块!");
        } finally {
            System.out.println("finally 代码块!");
            throw new Exception();
        }
    }

    public static void main(String[] args) throws Exception {

        FinallyDemo demo = new FinallyDemo();
        String ret = demo.finallyTest();
        System.out.println(ret);

    }
}
输出结果:
try 代码块!
finally 代码块!
Exception in thread "main" java.lang.Exception
	at com.example.demo.tool.demo.FinallyDemo.finallyTest(FinallyDemo.java:23)
	at com.example.demo.tool.demo.FinallyDemo.main(FinallyDemo.java:30)

Process finished with exit code 1

总结

以上就是关于finally的场景分析,尤其对于finally中有返回值又会影响到try返回结果的场景要重点关注
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值