关于try catch finally,请详解

lijin860725 2012-09-13 02:02:35
如果方法methedA()抛出一个IOException,程序的执行结果是?
class ExceptionTest{
public static void main(String args[]){

try{methodA();}
catch(IOException e){
System.out.println("caught IOException");
}
catch(Exception e){
System.out.println("caught Exception");
return;
}
finally{
System.out.println("catch one exception");
}
}

}
A caught IOException catch one exception
B caught IOException caught Exception
C caught Exception catch one exception
D caught Exception caught IOException catch One Exception
...全文
258 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsb_csdn 2012-09-13
  • 打赏
  • 举报
回复
如果在catch里面有System.exit(0)的话,还会执行finally里面的return吗??[Quote=引用 13 楼 的回复:]

执行完第一个catch(),还会执行finally(),finally()始终会执行
[/Quote]
lijin860725 2012-09-13
  • 打赏
  • 举报
回复
抛开这个题不说,如果catch里有return,是不是还是得执行finally?

[Quote=引用 13 楼 的回复:]

执行完第一个catch(),还会执行finally(),finally()始终会执行
[/Quote]
zhaoming262350 2012-09-13
  • 打赏
  • 举报
回复
执行完第一个catch(),还会执行finally(),finally()始终会执行
lijin860725 2012-09-13
  • 打赏
  • 举报
回复
???您确定?
[Quote=引用 10 楼 的回复:]

二楼说的不太全吧,如果catch中有return,就会直接跳出,不会走finally了
[/Quote]
ddplayer12 2012-09-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

第一个catch语句那有个红叉,编译提示的错误如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for IOException. This exception is never thrown from the try statemen……
[/Quote]

我这边运行没问题。
输出的是
caught IOException
catch one exception

是不是兄弟没有拷贝全???
fuguitong163 2012-09-13
  • 打赏
  • 举报
回复
二楼说的不太全吧,如果catch中有return,就会直接跳出,不会走finally了
achai123 2012-09-13
  • 打赏
  • 举报
回复
2楼加一个
licip 2012-09-13
  • 打赏
  • 举报
回复
2楼说的对。
小z123 2012-09-13
  • 打赏
  • 举报
回复
A 一楼说的很详细
xiars123 2012-09-13
  • 打赏
  • 举报
回复
你这个是try里面不会抛出异常,
methodA()这个方法需要throws IOException。

你照着4楼的代码敲进去便是,人家给你写了啊。
lijin860725 2012-09-13
  • 打赏
  • 举报
回复
第一个catch语句那有个红叉,编译提示的错误如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for IOException. This exception is never thrown from the try statement body
请问这是什么情况?
[Quote=引用 3 楼 的回复:]

A 楼上说得很全
给你测试代码你自己debug跑跑看

Java code

import java.io.IOException;

class ExceptionTest {
public static void main(String args[]) {

try {
methodA();
} catch (IOExc……
[/Quote]
菖蒲老先生 2012-09-13
  • 打赏
  • 举报
回复
A

catch(IOException e)完走finally

因为被第一个catch匹配到了,不会再走catch(Exception e)
蓝蜘蛛 2012-09-13
  • 打赏
  • 举报
回复
A 楼上说得很全
给你测试代码你自己debug跑跑看


import java.io.IOException;

class ExceptionTest {
public static void main(String args[]) {

try {
methodA();
} catch (IOException e) {
System.out.println("caught IOException");
} catch (Exception e) {
System.out.println("caught Exception");
return;
} finally {
System.out.println("catch one exception");
}
}

private static void methodA() throws IOException{

throw new IOException("error");
}


}
xiars123 2012-09-13
  • 打赏
  • 举报
回复
A
java的异常捕捉只要走到一个catch就不会走别的catch了
你的methedA()既然抛出了一个IOException,就会走到第一个catch block,然后到finally。
最后要注意的是,只要执行了try不论有没有异常,不论有没有catch到,都会执行finally中的语句
lijin860725 2012-09-13
  • 打赏
  • 举报
回复
请大侠指教~~~

62,614

社区成员

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

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