关于java中的try-catch-finally语句和return
来源:互联网 发布:linux打开二进制文件 编辑:程序博客网 时间:2024/06/12 16:02
看到一篇关于java中的try-catch-finally语句和return的博客,写的很好,总结的很全面,斗胆转载、分享一下:
原文出处:关于java中的try-catch-finally语句和return,作者博客:http://blog.zollty.com
第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂存在栈里面,等待finally执行后再返回)
第二:finally里面不建议放return语句,根据需要,return语句可以放在try和catch里面和函数的最后。可行的做法有四:
1.return语句只在函数最后出现一次。
2.return语句仅在try和catch里面都出现。
3.return语句仅在try和函数的最后都出现。
4.return语句仅在catch和函数的最后都出现。
注意,除此之外的其他做法都是不可行的,编译器会报错。
(1)如果程序运行到try成功时可以返回结果,则采用方法2。(见下面的例子test0_1,在那个例子中,方法2和4都是可行的,但是推荐方法2?)
(2)如果程序运行到catch时(即中途出错时)无需再继续执行后面的代码了,则采取方法4;(见下面例子中的test0,在那个特殊的例子中,只能采取方法4)
(3)如果程序运行到try或catch时还需要继续执行后面的代码,则采取方法1(见下面的例子test0_2,该例子只能采用方法1)。
下面是测试代码:
public class Test {public static void main(String[] args) {System.out.println("=============test1==================");System.out.println(test1());System.out.println("===============================");System.out.println("=============test1_1==================");System.out.println(test1_1());System.out.println("===============================");System.out.println("\n============test2===================");System.out.println(test2());System.out.println("===============================");System.out.println("\n============test2_1===================");System.out.println(test2_1());System.out.println("===============================");System.out.println("\n============test3===================");System.out.println(test3());System.out.println("===============================");System.out.println("\n============test3_1===================");System.out.println(test3_1());System.out.println("===============================");}public static String test0() {String a;int b;try {b = 8 / 0;} catch (Exception e) {return null;}a = String.valueOf(b);return a + b;}public static String test0_1() {String a;int b;try {b = 8 / 0;a = String.valueOf(b);return a + b;} catch (Exception e) {return null;}//return a+b; }public static String test0_2() {String a;int b = 0;try {b = 8 / 0;} catch (Exception e) {}a = String.valueOf(b);return a;}public static String test1() {String a = "in try";int n = -1;try {return a + n; //先执行这个,再执行finally } catch (Exception e) {} finally {//对String和int的更改均无效 a = "in finally";n = 123;System.out.println("do finally");}return a; //不会执行 } //总结出一点:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally public static String test1_1() {String a = "in try";try {return a;} catch (Exception e) {} finally { //从eclpise报警告可看出,finally里面不建议有return语句 a = "in finally";System.out.println("do finally");return a; //注释掉这句,eclipse将不再警告 }}public static int test2() {int a = 1;try {return a;} catch (Exception e) {} finally {a = 2;System.out.println("do finally");}return a;} //很显然,finally里面更改无效,返回的是a=1 public static int test2_1() {int a = 1;try {return a;} catch (Exception e) {} finally {a = 2;System.out.println("do finally");return a;}} //很显然,a取finally里面的值,a=2 //Helper类,将整数转换成字符串 static class Helper {int a;public String toString() {return String.valueOf(a);}}public static Helper test3() {Helper h = new Helper();h.a = 1;try {return h;} catch (Exception e) {} finally {h.a = 2; //对h.a的更改起作用!! //因为在try里面返回的是一个句柄,它指向的对象的内容 是可以改变的 System.out.println("do finally");}return h; //这个不会被执行 }public static Helper test3_1() {Helper h = new Helper();h.a = 1;try {return h;} catch (Exception e) {} finally {h.a = 2; //返回a=2,这个不用说了 System.out.println("do finally");return h;}}/** * 总结: * return语句,finally里面不建议放return语句,根据需要,可以放在try和catch里面 * */}
异常处理之思考
很少用自定义的异常类。
因为对于一般的异常根本无需进行多么复杂的处理,所以用不着自定义的异常类)。如果有自定义的异常类,一般当场就catch掉而不抛出(抛出意味着什么?抛出一般会涉及到一个向上转型的过程,比如,为了能处理所有的异常,通常throws Exception,这样就会把所有的异常子类都向上转型为Exception父类,就失去了自定义异常类的意义)。
但是实际上,对于异常,尽量做抛出处理,这样可以减少程序中到处都是try-catch。使用try-catch捕获异常时,建议一个try只放一行可能出现异常的代码,不要把很多无关的代码都写在try中(这是从编程的逻辑上而言,当多个异常集中在一起时,不妨只用一个try-catch。因为这样可以提高程序的效率,毕竟系统每处理一个try-catch语句都会有比较大的开销)。
- 关于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语句
- 关于try-catch-finally中的return语句解析
- return语句在Java的Try Catch Finally中的行为
- try...catch...finally中的执行顺序和return语句 总结
- 关于try catch finally 中的return问题
- 关于try-catch-finally中的return
- java try catch finally中的return问题
- Java中try-catch-finally中的return
- try...catch...finally中的return
- git https 转移到 ssh
- Java基础知识(一)
- Oracle之函数索引之各种列的函数转换
- laravel 问题 Class 'Predis\Client' not found
- 如何在shell脚本文件中获取awk的值, 存于变量中?
- 关于java中的try-catch-finally语句和return
- MyBatis面试题
- Jsp页面简单的Ajax提交
- S32K144 EVB之外部中断
- python 基本数据类型常用方法总结
- EasyNVR前端防止提交成功后多余操作提交
- window 下的 php 连接 操作 oracle 数据库
- Mobx和Mobe-React傻瓜式教程
- Sql Server服务远程过程调用失败解决