try-catch-finally

cocy_chan 2009-02-11 07:47:26
看了下try-catch-finally块的知识:
碰到了些问题:

public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
}

public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
}


上面的代码不会报错。
可是如果我的print()方法没有返回值,也就是说print()的代码写成:

public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
}
}

就会报错!!!
Thread.sleep(1)块就要捕捉InterruptedException这个异常,
这是为什么啊?
...全文
246 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
sosunny1018 2009-02-25
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 bzwm 的回复:]
当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
它还要去执行finally块中的代码。
因为如果此时抛出了,那么方法就退出了。但这时候finally块还未必执行。
而在finally块中恰好有return语句,所以方法正常退出。也就没有抛出异常。
[/Quote]
你这几句话我看了好几遍还没懂
能否再表述清晰一些?
sosunny1018 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 bzwm 的回复:]
如果在finally块中有return、continue语句,则会屏蔽掉抛出的异常。

讨论这个问题,首先要对try、catch、和finally的概念清楚才行,
而在这个帖子里,对finally的理解又更加重要。
finally不管出现异常与否,都必须去执行的代码。
因此在代码中,必须执行finally中的代码
PS:如果在try或者catch中强行终止JVM,就不会运行finally中的代码了。

然后再来看代码,
在你说的不会报错的情况下,
其实你把catch块注释掉也…
[/Quote]
确实是你说的在finally当中有了return的问题
不过为什么return会屏蔽掉异常呢
你的表述好混乱啊
ff9994 2009-02-13
  • 打赏
  • 举报
回复
up,顶一下
hello101105 2009-02-13
  • 打赏
  • 举报
回复
你的代码存在两个异常,你就捕获了一个,你打类应该继承Thread类。
cocy_chan 2009-02-13
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 ZiSheng 的回复:]
也就是说你的finally块加上return i;就不用捕捉InterruptedException,不加,就得捕捉InterruptedException,why?????
[/Quote]
就是啊,我也不是很懂!
bzwm 2009-02-13
  • 打赏
  • 举报
回复
当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
它还要去执行finally块中的代码。
因为如果此时抛出了,那么方法就退出了。但这时候finally块还未必执行。
而在finally块中恰好有return语句,所以方法正常退出。也就没有抛出异常。
bzwm 2009-02-13
  • 打赏
  • 举报
回复
如果在finally块中有return、continue语句,则会屏蔽掉抛出的异常。

讨论这个问题,首先要对try、catch、和finally的概念清楚才行,
而在这个帖子里,对finally的理解又更加重要。
finally不管出现异常与否,都必须去执行的代码
因此在代码中,必须执行finally中的代码
PS:如果在try或者catch中强行终止JVM,就不会运行finally中的代码了。

然后再来看代码,
在你说的不会报错的情况下,
其实你把catch块注释掉也没问题的。
解释一下这个屏蔽:
在这种情况下,有finally块,并且在try块中出现了异常,
那么,finally块里的语句,是在执行了catch语句之后、退出方法之前运行的。
先把代码这样改一下:

public void print() throws Exception {
int i = -1;
try {
Thread.sleep(1);
i = 1 / 0;
} catch (Exception e) {
System.out.println("catch step1.");
throw e;
} finally {
System.out.println("finally--- " + i);
return;
}
}

以上的代码, 当程序运行到catch块中throw e时,其实并没有抛出异常,而是先把它放在栈中,
因为如果此时抛出了,那么方法就退出了。
在finally块中有return语句,所以方法正常退出。也没有抛出异常。
PS:
1.你可以打印一下,在进入catch快后,打印了catch step1. 但并没有finally--- -1输出。
2.你可以运行一下,看是否这个被我改过的程序有异常抛出,肯定没有,而确实有异常产生了。
fanchangyong 2009-02-13
  • 打赏
  • 举报
回复
有点明白了,return写在finally里边就要以保证这个方法可以正常的return,所以不管出现任何异常程序也可以继续执行下去.
但是 return如果是写在外边,try块里边出了异常,程序是没办法执行的了.
cocy_chan 2009-02-12
  • 打赏
  • 举报
回复
别沉了哦~
mykelly6 2009-02-12
  • 打赏
  • 举报
回复
up
猿敲月下码 2009-02-12
  • 打赏
  • 举报
回复
public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
}

public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;//这里加个return
}
}
}
mike_24 2009-02-12
  • 打赏
  • 举报
回复
我的理解是这样的:有返回值,后面的代码就不会被执行,直接跳到finally的代码块里,就像for循环一样,有了返回值就不会再执行下去了.
ZiSheng 2009-02-12
  • 打赏
  • 举报
回复
也就是说你的finally块加上return i;就不用捕捉InterruptedException,不加,就得捕捉InterruptedException,why?????
ZiSheng 2009-02-12
  • 打赏
  • 举报
回复
[Quote=引用楼主 cocy_chan 的帖子:]
看了下try-catch-finally块的知识:
碰到了些问题:

Java code
public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
}
public int print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return i;
}
}
[/Quote]
你的finally块中的return i;去掉,就会出现和第二个没有返回值的print()一样的错误,
其实你的sleep()的异常InterruptedException是必须的捕捉的异常
豆虫 2009-02-12
  • 打赏
  • 举报
回复
��6¥
cocy_chan 2009-02-11
  • 打赏
  • 举报
回复
现在代码是这样,没有返回值return,不知道为什么会报错。
而有返回值又不会报错呢。

public void print()
{
int i = -1;
try
{
Thread.sleep(1);
//i = 1 / 0;
//return i;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
//return i;
}
}

输得代码会报错。
而如果最后一句的//return i;修改为return就不会报错了。
不知道为什么
Person C 2009-02-11
  • 打赏
  • 举报
回复
你try里面是Thread
所以catch 的就一定要是InterruptedException

IndexOutOfBoundsException和ArithmeticException是运行期异常,
yyqbest 2009-02-11
  • 打赏
  • 举报
回复
public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;
}
}

这样就不会报错了

爱摸鱼de老邪 2009-02-11
  • 打赏
  • 举报
回复
跟return有关。你把代码改成这样也不会报错的。

public class FinallyTest
{
public static void main(String[] args)
{
//System.out.println("main--- " + new FinallyTest().print());
new FinallyTest().print();
}

public void print()
{
int i = -1;
try
{
Thread.sleep(1);
i = 1 / 0;
//return;这个可写可不写
}
catch (IndexOutOfBoundsException e)
{
}
finally
{
System.out.println("finally--- " + i);
return;
}
}
}
但是如果把return写在try-catch-finally块外面,还是需要你捕捉InterruptedException的。
cocy_chan 2009-02-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yewei2008 的回复:]
你Catch的异常也写错了
[/Quote]
我故意把异常写成IndexOutOfBoundsException的,
我理解的是:只要是RuntimeException的话,放在catch()中,在编译时都是可以的。
这里IndexOutOfBoundsException和ArithmeticException的效果应该是一样的。
加载更多回复(4)

62,614

社区成员

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

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