Exception
来源:互联网 发布:js onclick传参 编辑:程序博客网 时间:2024/06/11 19:33
下面代码输出什么?
package
algorithms.com.guan.javajicu;
public
class
TestDemo
{
public
static
String output = ””;
public
static
void
foo(inti)
{
try
{
if
(i ==
1
)
{
throw
new
Exception();
}
}
catch
(Exception e)
{
output += “
2
”;
return
;
}
finally
{
output += “
3
”;
}
output += “
4
”;
}
public
static
void
main(String[] args)
{
foo(
0
);
foo(
1
);
System.out.println(output);
}
}
首先,finally语句必执行。
无异常,那么执行try,再finally。由于finally必执行,所以当try有跳转语句return时,会先执行finally再跳转。
有异常并抛出,则try catch finally,同样finally会在catch中的return之前执行。
输出:3423
注:return后,函数已经跳出,不会再执行
output += “
4
”; 。
下面代码输出什么?
public
static
int
func (){
try
{
return
1;
}
catch
(Exception e){
return
2;
}finally{
return
3;
}
}
答:
当try执行到跳转语句时,会先执行finally释放资源,再跳转。
输出:3
0 0
- Exception
- Exception
- Exception
- exception
- Exception
- Exception
- Exception
- exception
- Exception
- Exception
- Exception
- Exception
- exception
- exception
- exception
- Exception
- Exception
- Exception
- c++-----强制类型转换
- 当前不会命中断点 还没有为该文档加载任何符号
- spring 配置事务传播特性
- 模仿淘宝评价的星级显示
- iPhone 应用中实现拨打电话功能的代码
- Exception
- C语言中,为什么字符串可以赋值给字符指针变量
- 安卓中关于图片从网络获取,压缩,上传,下载,缩略图,缓存的一些处理总结(四)
- JVM 学习笔记---Java虚拟机栈
- 自定义控件添加上自定义的控件属性。
- Android 一个绚丽的loading动效分析与实现!
- 矩阵的等价,相似,合同
- 好用的调用图库和拍照并裁剪功能(适用于上传头像)
- Java接口回调技术