try-catch-finally

cocy_chan cocy_chan 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
看了下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这个异常,
这是为什么啊?
...全文

相关推荐

评论 24
收藏 已收藏
分享
返回顶部
cocy_chan
Java SE 62,620 成员 307,251 内容