关于try、catch、finally语句块中含有return语句的几点说明
来源:互联网 发布:mac没声音 编辑:程序博客网 时间:2024/06/01 19:56
1、第一种情况:try块有return语句,catch块没有return,函数末尾也没有return:
看代码:
import java.util.*;
public class Demo
{
public static void main(String args[])
{
int num = 10;test(num);}public static int test(int b)
{
try
{
b += 10;return b;
}}
catch(Exception e){
}
finally{
}}
编译结果:
H:\java Demo>javac Demo.java
Demo.java:18: 缺少返回语句
}
^
1 错误
有人可能会说,我在try块中不是有return语句吗?为什么会提示缺少return语句呢?这是因为编译器认为try块中是又可能产生异常操作
的,也就是说在return语句之前如果出现异常的话,那么return语句根本没有机会得到执行,所以编译器会认为缺少return语句。
解决办法:a、在catch块中加入return语句,因为一旦出现异常,catch中的语句可以保证函数会有一个返回值
b、在finally块中加入return语句,同样只要系统不退出,finally语句块会始终得到执行的
代码:
import java.util.*;
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;return b;}
catch(Exception e)
{
}
finally
{
return 0;}
}}
c、在函数末尾加入return语句
代码:
import java.util.*;
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;return b;
}}
catch(Exception e)
{
}
finally
{
}return 0;}
2、第二种情况,看代码:
import java.util.*;
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;
}return 0;
}}
结果:H:\java Demo>javac Demo.java
Demo.java:8: "try" 不带有 "catch" 或 "finally"
try{
^
1 错误
说明:也就是说,如果有try语句的话,可以有catch语句,没有finally语句,但是如果没有catch语句的话,那么一定要有finally语句
。并且如果出现catch语句的话,catch语句可以出现多次,而finally语句只能出现一次。
代码:
public class Demo
{
}public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;}
catch(RuntimeException e)
{
}
catch(Exception e2)
{
}
finally
{
}return 0;}
3、第三种情况:
a、try块中有return语句,那么是先执行return语句,还是先执行finally语句。大家通常会有一个错误的认识,可能有的老师都会讲错
,认为是先执行finally语句,再执行return语句,但是这是错误的,事实上是先执行return语句,再执行finally语句,然后将结果返回,也可以说return语句执行了两次,一次在finally之前,一次在finally之后,但是返回的确是第一次执行的值
,如果有不信的,请继续看代码,此段代码可以证明我的观点:
代码:
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;return b;}
catch(RuntimeException e)
{
}
catch(Exception e2)
{
}
}finally
{
b += 10;}return 0;}
结果:
H:\java Demo>javac Demo.java
H:\java Demo>java Demo
20
说明:此处需要引入缓冲的概念,有对缓冲不太了解的也没关系,程序运行结果是20足以证明我的观点,程序执行流程是进入try语句块
执行return语句,但是程序有finally语句块,所有先将return返回的值缓冲起来,然后程序跳转到finally语句块执行,我在finally语
句块中修改了变量b的值,但是程序返回的依然是20,并不是30,说明finally语句块执行完毕后,程序直接将之前缓冲的值返回了。所以
这就是真实的执行流程。
b、try块中有return,finally语句块中也有return,此时的情况是什么样呢?
看代码:
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
b += 10;return b;}
catch(RuntimeException e)
{
}
catch(Exception e2)
{
}
finally
{
b += 10;return b;}
}}
结果是:
H:\java Demo>java Demo
30
说明:为什么此时结果是30了呢,大家都知道return语句的作用是结束程序体,所以此段代码依然是先执行try块中的return语句,并将20
缓存起来,接着跳转到finally语句块执行,但是由于finally语句块中有return语句,所以程序在此处结束,并不返回到try块中返回结
果,而是直接将finally块中新修改的值即30,返回。
c、try块中有return语句,catch块中也有return语句,这时候是最简单的一种情况:
看代码:
public class Demo
{
public static void main(String args[])
{
int num = 10;System.out.println(test(num));}public static int test(int b)
{
try
{
int a = b/0;return b;}
catch(Exception e2)
{
b += 10;return b;
}}
}
结果:
H:\java Demo>java Demo
20
说明:我在try块中触发了一个异常,程序跳入catch语句块,try语句块中剩下的所以语句均没有执行的机会,接着,我在catch块中修改
了b的值,此时b应该是20,然后将b值返回,与运行结果相符。
以上几点基本涵盖了大部分的情况,掌握这些知识点基本可以应付多数此类问题,如果有什么错误,遗漏欢迎指正。
转自:http://blog.sina.com.cn/s/blog_6aefe42501018wmw.html
- 关于try、catch、finally语句块中含有return语句的几点说明
- 关于try、catch、finally语句块中含有return语句的几点说明
- 关于try、catch、finally语句块中含有return语句的几点说明
- 关于try、catch、finally语句块中含有return语句的几点说明
- try、catch、finally语句块中含有return
- try catch finally语句中含有return语句的执行情况
- 含有return语句的try-catch-finally语句执行顺序
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- Java中try catch finally语句中含有return语句的执行情况(总结版)
- 微信小程序页面跳转传参填充数据的小案例
- oracle cols 查询表字段类型及长度 function
- windows系统下的keras(Theano/Tensorflow后端)CPU以及GPU加速环境搭建
- C# ArrayList、HashSet、HashTable、List、Dictionary的区别
- 《学习opencv》课后题:4.1.c如何显示文字标签?
- 关于try、catch、finally语句块中含有return语句的几点说明
- centos安装nginx,配置负载均衡
- 整合SSH框架的思路及方法
- 最小费用最大流背诵用模板
- Leetcode Tree Problem 树问题合集
- 用FlexboxLayout构建灵活的布局
- Intellij IDEA创建基于Gradle的SpringMVC工程
- 分布式TensorFlow集群使用入门
- 数据传输加密——非对称加密算法RSA+对称算法AES(适用于java,android和Web)