Java中,执行try-catch-finally语句需要注意:

第一: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。

  (2)如果程序运行到catch时(即中途出错时)无需再继续执行后面的代码了,则采取方法4。

  (3)如果程序运行到try或catch时还需要继续执行后面的代码,则采取方法1。

package cn.javaoppday01;

public class Text {

    public static void main(String[] args) {
System.out.println(getResult());
}
public static int getResult(){
int a =;
try{
a++; //此行执行的是try 原值为100,执行try的时候为101
return a;
}finally{
a++;
}
} }
结果为101
package cn.day006;

public class Test2 {
public static void main(String[] args) {
try{
System.out.println(getResult());
}catch(Exception e){
e.printStackTrace();
System.out.println("截获异常catch");
}finally{
System.out.println("异常处理finally");
}
} @SuppressWarnings("finally")
public static int getResult() throws Exception{
int a =;
try{
a=a+;
throw new RuntimeException();
}catch(Exception e){
System.out.println("截获异常并重新抛出异常");
throw new Exception();
}finally{
return a;
}
}
}
结果为:
截获异常并重新抛出异常
110
异常处理finally

即使在try块,catch块中存在return语句,finally块中的语句也会执行,finally 块中的语句不执行的唯一情况是,在异常处理代码中执行System.exit(1);

在try--catch -finally 中 如果有return关键字的话  执行顺序如下:

try --catch--finally -return

异常处理机制中的return关键字的更多相关文章

  1. 论 异常处理机制中的return关键字

    Java中,执行try-catch-finally语句需要注意: 第一:return语句并不是函数的最终出口,如果有finally语句,这在return之后还会执行finally(return的值会暂 ...

  2. Scala 中为什么不建议用 return 关键字

    在scala中使用 return 的话,编译的时候会提示the latest statement is method is automatically returned, use of th retu ...

  3. return 关键字的作用

    注意: 如果一个函数的返回值类型是具体的数据类型,那么该函数就必须要保证在任意情况下都保证有返回值.(除了返回值类型是void以外) return 关键字的作用: 1. 返回数据给函数的调用者. 2. ...

  4. Nginx return 关键字配置小技巧

    Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...

  5. 用return关键字实现1——100累加求和,返回总和并接收输出

    package com.Summer_0419.cn; /** * @author Summer * 用return关键字实现1——100累加求和,返回总和并接收输出 */ public class ...

  6. 用return关键字实现求和操作

    package com.Summer_0419.cn; /** * @author Summer * 用return关键字的知识,实现求和操作 */ public class Test_Method0 ...

  7. return关键字的作用和接受实验

    package com.Summer_0419.cn; /** * @author Summer * 查看return关键字的作用,实验目的: * 1.传入两个实参查看输出结果 * 2.传入两个无参数 ...

  8. 异常 try catch finally return 执行关系 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. “全栈2019”Java异常第九章:throws关键字详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

随机推荐

  1. Asp.net core 学习笔记 ( User Secrets )

    参考 : http://cnblogs.com/nianming/p/7068253.html https://docs.microsoft.com/en-us/aspnet/core/securit ...

  2. (转)c# 扩展方法

    扩展方法能够向现有类型“添加”方法,而无需创建新的派生类型,重新编译或以其他方式修改原始类型.扩展方法必须是静态方法,可以像实例方法一样进行调用.且调用同名中实际定义的方法优先级要高于扩展方法. 先来 ...

  3. Python自学:第二章 数字 整数

    >>>2 + 3 5 >>>3 - 2 1 >>>3 * 2 6 >>>3 / 2 1.5

  4. linux 下如何安装memcached 和启动服务

    一.安装gcc # yum -y install gcc 二.安装libevent # wget http://www.monkey.org/~provos/libevent-2.0.12-stabl ...

  5. php数组的逐行写入文件与读取

    <?php /** * * 对数组$arr1=['Apple Orange Banana Strawberry'] 写入文件,并读取 **/ class IoFile { private $pa ...

  6. ansible管理windows (发送文件)

    https://github.com/ansible/ansible/raw/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 环境: 服务 ...

  7. 5月13 jquery的一些应用

    首先对于JavaScript的一些复习:操作内容,操作属性,操作样式 <title>无标题文档</title> <style> #aa { width:200px; ...

  8. Matlab:非线性热传导(抛物方程)问题

    函数文件1:real_fun.m function f=real_fun(x0,t0) %精确解 f=4*x0*(1-x0)*sin(t0); 函数文件2:F.m function f=F(N,u,U ...

  9. DOM与document的区别

    DOM: DOM 全称是 Document Object Model,也就是文档对象模型. DOM 就是针对 HTML 和 XML 提供的一个API.什么意思?就是说为了能以编程的方法操作这个 HTM ...

  10. bootstrap fileinput组件的使用

    组件的下载地址为:https://github.com/kartik-v/bootstrap-fileinput 比较详细的介绍可参看:http://www.jq22.com/jquery-info5 ...