Java中finally和return的关系实例解析
作者:司马懿字仲达 时间:2023-05-31 04:21:31
本文研究的主要是Java中finally和return的关系,具体介绍和实例如下所示。
finally 和 return 关系的总结
1.try块中有System.exit(0)这样的语句,由于它是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
2.其它情况下,finally语句都必然会被执行。因此可以在这里执行一些资源的释放操作。
(1)finally中的return会覆盖try/catch中的renturn。
(2)在finally中写return语句会有警告,因为它会阻止函数抛出异常,而改为正常返回。
package com.demo.test;
public class FinallyAndReturn {
private static void finallyAndTryReturn() {
try {
System.out.println("finallyAndTryReturn -> try");
return;
}
catch (Exception e) {
System.out.println("finallyAndTryReturn -> catch");
}
finally {
System.out.println("finallyAndTryReturn -> finally");
}
}
private static void finallyAndCatchReturn() {
try {
System.out.println("finallyAndCatchReturn -> try");
throw new Exception();
}
catch (Exception e) {
System.out.println("finallyAndCatchReturn -> catch");
return;
}
finally {
System.out.println("finallyAndCatchReturn -> finally");
}
}
// finally语句是在try的return语句执行之后,return返回之前执行。
private static String tryReturn() {
String str = "initialized";
try {
System.out.println("tryReturn -> try");
str = "try";
return str;
}
catch (Exception e) {
System.out.println("tryReturn -> catch");
str = "catch";
}
finally {
System.out.println("tryReturn -> finally");
str = "finally";
}
return null;
}
private static String tryReturnAndFinallyReturn() {
String str = "initialized";
try {
System.out.println("tryReturnAndFinallyReturn -> try");
str = "try";
return str;
}
catch (Exception e) {
System.out.println("tryReturnAndFinallyReturn -> catch");
str = "catch";
}
finally {
System.out.println("tryReturnAndFinallyReturn -> finally");
/*
* Warning: finally block does not complete normally
* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
*/
str = "finally";
return str;
}
}
private static String tryThrowAndFinallyReturn() throws Exception {
String str = "initialized";
try {
System.out.println("tryThrowAndFinallyReturn -> try");
str = "try";
throw new Exception();
}
catch (Exception e) {
System.out.println("tryThrowAndFinallyReturn -> catch");
str = "catch";
throw new Exception();
}
finally {
System.out.println("tryThrowAndFinallyReturn -> finally");
/*
* Warning: finally block does not complete normally
* 如果finally块中包含了return语句,即使前面的catch块重新抛出了异常,则调用该方法的语句也不会获得catch块重新抛出的异常,而是会得到finally块的返回值,并且不会捕获异常。
*/
str = "finally";
return str;
}
}
private static void finallyAndRuntimeException() {
try {
System.out.println("finallyAndRuntimeException -> try");
throw new RuntimeException();
}
catch (Exception e) {
System.out.println("finallyAndRuntimeException -> catch");
}
finally {
System.out.println("finallyAndRuntimeException -> finally");
}
}
private static void finallyAndExit() {
try {
System.out.println("finallyAndExit -> try");
// System.exit(0);是终止Java虚拟机JVM的,连JVM都停止了,所有都结束了,当然finally语句也不会被执行到。
System.exit(0);
}
catch (Exception e) {
System.out.println("finallyAndExit -> catch");
}
finally {
System.out.println("finallyAndExit -> finally");
}
}
public static void main(String[] args) {
finallyAndTryReturn();
System.out.println();
finallyAndCatchReturn();
System.out.println();
System.out.println("tryReturn return -> " + tryReturn());
System.out.println();
System.out.println("tryReturnAndFinallyReturn return -> " + tryReturnAndFinallyReturn());
System.out.println();
try {
System.out.println("tryThrowAndFinallyReturn return -> " + tryThrowAndFinallyReturn());
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println();
finallyAndRuntimeException();
System.out.println();
finallyAndExit();
}
}
演示结果:
总结
来源:http://blog.csdn.net/chy555chy/article/details/52791338
标签:java,finally,return
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Flutter实现图片选择和图片上传
2023-07-06 04:28:50
![](https://img.aspxhome.com/file/2023/1/106021_0s.jpg)
JavaSE实现猜拳游戏
2023-12-22 07:21:19
C#实现自定义光标并动态切换
2021-09-25 09:06:28
![](https://img.aspxhome.com/file/2023/5/113855_0s.gif)
Java Mybatis中的 ${ } 和 #{ }的区别使用详解
2021-07-02 22:35:27
Android自定义控件实现时间轴
2021-07-12 04:13:08
浅谈Maven镜像更换为阿里云中央仓库(精)
2022-08-06 04:48:17
Android 多媒体播放API简单实例
2022-12-12 18:53:24
![](https://img.aspxhome.com/file/2023/2/110742_0s.png)
android仿音悦台页面交互效果实例代码
2023-03-27 12:55:54
![](https://img.aspxhome.com/file/2023/4/138174_0s.gif)
Java创建线程的五种写法总结
2023-01-10 04:39:34
spring boot集成smart-doc自动生成接口文档详解
2023-11-28 23:08:02
![](https://img.aspxhome.com/file/2023/3/60773_0s.png)
浅谈Android View绘制三大流程探索及常见问题
2022-03-02 00:01:26
Android刮刮卡功能具体实现代码
2021-08-06 04:12:24
![](https://img.aspxhome.com/file/2023/0/88670_0s.jpg)
Java面向对象选择题总结归纳
2023-08-05 23:30:21
c#源码的执行过程详解
2023-10-01 17:03:37
![](https://img.aspxhome.com/file/2023/5/113725_0s.png)
C#实现抓取和分析网页类实例
2023-09-21 04:27:32
Android实现点击获取验证码倒计时效果
2022-08-29 09:23:41
Java基础知识之ByteArrayInputStream流的使用
2023-10-27 14:37:53
Android使用ListView批量删除item的方法
2022-05-01 01:11:17
![](https://img.aspxhome.com/file/2023/7/138547_0s.jpg)
Spring中SmartLifecycle和Lifecycle的作用和区别
2023-11-18 22:55:50
代理模式:JAVA静态代理和动态代理的实例和实现详解
2023-06-05 02:19:27
![](https://img.aspxhome.com/file/2023/5/103825_0s.png)