throw和finally同时用的问题

ningzhengping 2008-08-28 11:14:53
public void test() throws Exception{
try{
...
}catch(Exception e){
throw e;
}finally{
...
}
}

如下形式调用test()方法:
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
输出结果为空,如果将test方法中的finally块去掉就能输出错误信息,这是怎么回事啊?
...全文
1163 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zloole 2021-06-30
  • 打赏
  • 举报
回复

因为throw必须最后执行,而finally又必须执行,为了保证这个规则,所以可能是先执行的try,然后执行catch,发现是throw,就先执行finally里的内容。而且得保证从上至下得执行规则,应该底层代码用了临时变量。类似于i = i++ 这种问题。你说去掉finally就正常了,那说明finally里的内容对throw产生了影响。

asdpoi 2011-10-31
  • 打赏
  • 举报
回复
楼主给错分 #6和#11楼才是正确
meteor57 2008-08-30
  • 打赏
  • 举报
回复
昨天回复(6#)有点问题.(把它想成一个try多个catch了)
貌似有2个 try 就要 catch 2次...去不去掉 finally 都没什么问题.

public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
finally
{
System.out.println("finally");
}
}

public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
/*
output:
finally
Do you see me ?
*/



public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
throw e;
}
/*finally
{
System.out.println("finally");
}*/
}

public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:Do you see me ?


如果没有把异常抛出,异常被test()的try吞了情况就不一样了.


public class Test
{
void test() throws Exception
{
try
{
int a = 1/0;
}catch(Exception e){
//throw e;
}
/*finally
{
System.out.println("finally");
}*/
}

public static void main(String[] args)
{
try
{
new Test().test();
}
catch (Exception e)
{
System.out.println("Do you see me ?");
}
}
}
//output:(什么也没有...)

huoyin 2008-08-29
  • 打赏
  • 举报
回复
这个是因为你的try block中没有一场被抛出。手工加一个一场测试一下

public void test() throws Exception{
try{
...
throw new Exception("See me?"); }catch(Exception e){
throw e;
}finally{
...
}
}
meteor57 2008-08-29
  • 打赏
  • 举报
回复
这样相当于一个嵌套try.
内层(test())try产生异常.内层catch已经捕获,外层
catch(Exception e){System.out.println(e.getMessage());}
就不会再捕获执行了.
去掉finally也不会捕获执行.
aniude 2008-08-29
  • 打赏
  • 举报
回复
我仿佛看不懂楼主的意思 :(
ningzhengping 2008-08-29
  • 打赏
  • 举报
回复
多谢各位,分太少,不能人人都给到了!
cwjieNo1 2008-08-29
  • 打赏
  • 举报
回复
同意楼上的见解~~
gesanri 2008-08-29
  • 打赏
  • 举报
回复
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
这里执行到test()时执行下面的test函数
public void test() throws Exception{
try{
...
}catch(Exception e){
throw e;
}finally{
...
}
}
而这个test函数在执行完finally里后程序就结束了,它不会返回到下面程序中的catch再执行,所以不会有输出,当你去掉了finally后它就会再执行完上面函数的方法后继续执行下面程序中的catch语句,就会返回e.getMseesage()的值了
try{
test();
}catch(Exception e){
System.out.println(e.getMessage());
}
ZangXT 2008-08-28
  • 打赏
  • 举报
回复

public static int test(){
try{
return 1;
}
catch(Exception e){

}
finally{
return 2;
}
}

这个更能说明问题.
ZangXT 2008-08-28
  • 打赏
  • 举报
回复
你在try或者catch加个return语句也有这样的问题
ZangXT 2008-08-28
  • 打赏
  • 举报
回复
finally总会执行,throw语句将结束方法的执行,但是指向完了throw语句之后,控制权就转到finally了,throw语句就起不到抛出异常结束方法的作用了
rzg2005 2008-08-28
  • 打赏
  • 举报
回复
可能是finally块里面出异常吧。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧