try-catch

dy15 2003-05-03 01:36:58
import java.io.IOException;
class SubEx extends IOException { }
class A {
public static void main(String[] args) {
try {
thud();
} catch (SubEx x) {
System.out.println("main() caught SubEx");
} catch (IOException x) {
System.out.println( "main() caught IOException");
} catch (Exception x) {
System.out.println( "main() caught Exception");
}
}
static void thud() throws IOException {
try {
throw new SubEx();
} catch (SubEx x) {
System.out.println("thud() caught SubEx");
throw new IOException();
////////这儿怎么没有抛给下面的System.out.println( "thud() caught IOException");

} catch (IOException x) {
System.out.println( "thud() caught IOException");
throw new IOException();
} catch (Exception x){
System.out.println( "thud() caught Exception");
}
}
}

结果是:
thud() caught SubEx
main() caught IOException
...全文
24 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Iforgot 2003-05-03
  • 打赏
  • 举报
回复
这样的:
你抛出SubEx()例外对象,所以你的catch捕获了,但是你的捕获catch (SubEx x)的抛出并没有try去捕获。而是被函数抛出了,准备给try他的块去捕获。

因为你只抛出了SubEx例外,所以,你的catch (IOException x)、catch (Exception x)当然都没的捕获了。 你的这3个捕获都是对你的try而言的,后面的也不是对你前面的catch用的。

注意try和catch的配对问题。 建议仔细理解学习一下例外机制。
moxie 2003-05-03
  • 打赏
  • 举报
回复
不好意思,发错了!嘿嘿!
moxie 2003-05-03
  • 打赏
  • 举报
回复
Jbuilder中生成可运行文件步骤:

执行步骤:(有问题再给我email)
1、Wizards->Native Executable Builder
2、弹出一个窗口(左上角有"-Stem 1 of 6")点击next
3、弹出一个新窗口(左上角有"-Stem 2of6")单选框根据需要选中 点击"AddClasses",将你的所 有要打包的文件都加进去。点击next
4、弹出一个新窗口(左上角有"-Stem 3of 6") 点击next
5、弹出一个新窗口(左上角有"-Stem 4of 6") 点击next
6、弹出一个新窗口(左上角有"-Stem 5of 6")
最好点第二个复选框(Use the class specified...),点右边的浏览按扭,在弹出的窗口中选Browse,选择你的有main()函数,可以运行的文件。点击next
7、弹出一个新窗口(左上角有"-Stem 6of 6")点击finesh
8、现在在左边的显示框中发现多了一个东东,它的图标和Wizards->Native
Executable Builder菜单上的图标一样。选种它,点击右键,执行"make"。
9、回到你的project文件目录,呵呵 爽!
leejidongdong 2003-05-03
  • 打赏
  • 举报
回复
只有一个catch会被进入!
telenths 2003-05-03
  • 打赏
  • 举报
回复
在一连串 cactch 中 只会有一个 catch 被执行
是 catch 的 Exception 与 抛出的 Exception 最接近的那个

之后就会跳出 try - catch 执行 finally 或 下面的内容
wyy_9715072 2003-05-03
  • 打赏
  • 举报
回复
throw相当于return,欧以前也是和 dy15(知足常乐!)相同的看法,不过,自己写了例子测试了一下,发现不是这样的,想想也是,它既然已经return了,怎么还会被下面的捕获呢。收获颇丰
祝节日快乐
CyberH 2003-05-03
  • 打赏
  • 举报
回复
我的意思是说try和catch的关系相当于switch和case这样的条件选择关系

static void thud() throws IOException {
try {
throw new SubEx();
} catch (SubEx x) {
System.out.println("thud() caught SubEx");
throw new IOException();//执行完这句就退出thud了,不会接着执行下面的catch语句,所以这个IO错误就只能在main里面被捕获~~~
} catch (IOException x) {
System.out.println( "thud() caught IOException");
throw new IOException();
} catch (Exception x){
System.out.println( "thud() caught Exception");
}
}
dy15 2003-05-03
  • 打赏
  • 举报
回复
to: CyberH()
try和catch是根据条件选择执行的

System.out.println("thud() caught SubEx");
throw new IOException();
////////这儿怎么没有抛给下面的System.out.println( "thud() caught IOException");
想问一下.throw new IOException();算不算条件????
mercury1231 2003-05-03
  • 打赏
  • 举报
回复
因为你声明了static void thud() throws IOException 啦,

这个IOException 被选择 沿调用栈回溯到调用者方法 抛出,而不是在这个方法中继续执行被捕捉。
CyberH 2003-05-03
  • 打赏
  • 举报
回复
try和catch是根据条件选择执行的,不会把每个catch都执行一遍~~~

62,614

社区成员

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

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