try--catch--finally中return返回值执行的顺序
public static int NoException(){
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
return --i;
}
}
public static void main(String[] args) {
System.out.println("=============NoException==================");
System.out.println(NoException());
System.out.println("===============================");
}
=============NoException==================
i in try block is:10
i in finally - from try or catch block is:9
8
===============================
public static int NoException1(){
int i=10;
try{
System.out.println("i in try block is:"+i);
return --i;
}
catch(Exception e){
--i;
System.out.println("i in catch - form try block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
}
=============NoException1==================
i in try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
public static int WithException(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i = i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
--i;
System.out.println("i in catch block is:"+i);
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is--"+i);
--i;
System.out.println("i in finally block is--"+i);
return --i;
}
}
=============WithException==================
i in try block is:10
i in catch - form try block is:10
i in catch block is:9
i in finally - from try or catch block is--8
i in finally block is--7
6
===============================
public static int WithException1(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
return --i;
}finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
System.out.println("i in finally block is:"+i);
//return i;
}
}
=============WithException1==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:9
i in finally block is:8
9
===============================
public static int WithException2(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
int j = i/0;
return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
return --i;
}
=============WithException2==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
public static int WithException3(){
int i=10;
try{
System.out.println("i in try block is:"+i);
i=i/0;
//return --i;
}
catch(Exception e){
System.out.println("i in catch - form try block is:"+i);
//int j = i/0;
//return --i;
}
finally{
System.out.println("i in finally - from try or catch block is:"+i);
--i;
--i;
System.out.println("i in finally block is:"+i);
//return --i;
}
return --i;
}
=============WithException3==================
i in try block is:10
i in catch - form try block is:10
i in finally - from try or catch block is:10
i in finally block is:8
7
===============================
try--catch--finally中return返回值执行的顺序的更多相关文章
- try--catch--finally中return返回值执行的顺序(区别)
1.try块中没有抛出异常,try.catch和finally块中都有return语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public static int ...
- 用jquery的ajax方法获取return返回值的正确姿势
如果jquery中,想要获取ajax的return返回值,必须注意两方面,ajax的同步异步问题,在ajax方法里面还是外面进行return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第 ...
- 用jquery的ajax方法获取不到return返回值
如果jquery中,获取不到ajax返回值. 两个错误写法会导致这种情况:1.ajax未用同步 2.在ajax方法中直接return返回值. 下面列举了三种写法,如果想成功获取到返回值,参考第三种写法 ...
- 关于ExecuteNonQuery执行存储过程的返回值 、、实例讲解存储过程的返回值与传出参数、、、C#获取存储过程的 Return返回值和Output输出参数值
关于ExecuteNonQuery执行存储过程的返回值 用到过ExecuteNonQuery()函数的朋友们在开发的时候肯定这么用过. if(cmd.ExecuteNonQuery("xxx ...
- c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题
c++中带返回值函数没写return能通过编译但运行时会出现奇怪问题 例如: string myFunc(){ theLogics(); } 发现调用: myFunc(); 崩溃. 但调用: cout ...
- C# 调用存储过程操作 OUTPUT参数和Return返回值
本文转载:http://www.cnblogs.com/libingql/archive/2010/05/02/1726104.html 存储过程是存放在数据库服务器上的预先编译好的sql语句.使用存 ...
- [改善Java代码]不要在finally块中处理返回值
在finally代码块中处理返回值,这是在面试题中经常出现的题目.但是在项目中绝对不能再finally代码块中出现return语句,这是因为这种处理方式非常容易产生"误解",会严重 ...
- 字节码分析finally块对return返回值的影响
直接进入主题.看如下代码: public int test(){ int i=0; try { i=1; return i; } catch (Exception e) { i=2; return i ...
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
随机推荐
- express koa koa2 优缺点分析
发布日期 2009年6月26日,TJ 提交 Express 第一次 commit.目前拥有 5000 多次 commit. 2013年8月17日, TJ 只身一人提交 Koa 第一次 commit.目 ...
- ps -ef|grep ?解释
上述内容为: 命令拆解: ps:将某个进程显示出来-A 显示所有程序. -e 此参数的效果和指定"A"参数相同.-f 显示UID,PPIP,C与STIME栏位. grep命令是查找 ...
- “由于无法验证发行者,所以WINDOWS已经阻止此软件”的解决方法
Vista 和 Windows7 系统都很注重系统的安全性,在提高安全性的同时,也给我们某些应用带来不便,例如需要安装插件或证书,可能会弹出“由于无法验证发行者,所以WINDOWS已经阻止此软件”的相 ...
- .NetCore(四) 在Nginx部署
本篇主要体验一下Nginx的使用,之前只接触过IIS. 一.Nginxa) ASP.NET Core内置了Kestrel服务器,但功能简单,主要用于SelfHost,正式运行还是要依赖IIS.Apa ...
- Android RecycleView 自定义Item的使用
自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBin ...
- MYSQL获取当前年、季、月、周第一天、最后一天的日期/时间戳
因为做一些商场某个会员今年的消费分析,所以对sql中时间的获取进行了判断. 例如获取今年(即当前年的第一天到昨天0时之间)的消费总额. 如果需要时间戳转换,用UNIX_TIMESTAMP()函数. 一 ...
- Spark程序数据结构优化
场景: 1.scala中的对象:对象头是16个字节(包含指向对象的指针等源数据信息),如果对象中只有一个int的属性,则会占用20个字节,也就是说对象的源数据占用了大部分的空间,所以在封装数据的时候尽 ...
- [20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED2.txt
[20180317]12c TABLE ACCESS BY INDEX ROWID BATCHED2.txt --//简单探究12c TABLE ACCESS BY INDEX ROWID BATCH ...
- php报错 【 Call to undefined function imagecreatetruecolor()】
刚才在写验证码的时候,发现报错,然后排查分析了一下,原来是所用的php版本(PHP/5.3.13)没有开启此扩展功能. 进入php.ini 找到extension=php_gd2.dll ,将其前面的 ...
- Django 项目连接数据库Mysql要安装mysqlclient驱动出错 : Failed building wheel for mysqlclient:
1,如果直接用 CMD命令:pip install mysqlclient ,会安装出错. 2,解决问题,参考了这个博友的帖子:https://blog.csdn.net/qq_29784441/ar ...