CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  J2SE / 基础类

try-catch

楼主dy15(知足常乐!)2003-05-03 01:36:58 在 Java / J2SE / 基础类 提问

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 问题点数:50、回复次数:10Top

1 楼CyberH()回复于 2003-05-03 01:44:58 得分 10

try和catch是根据条件选择执行的,不会把每个catch都执行一遍~~~Top

2 楼mercury1231(为论文而拼搏,老板就是上帝)回复于 2003-05-03 01:59:44 得分 10

因为你声明了static   void   thud()   throws   IOException   啦,  
   
  这个IOException   被选择   沿调用栈回溯到调用者方法   抛出,而不是在这个方法中继续执行被捕捉。Top

3 楼dy15(知足常乐!)回复于 2003-05-03 02:01:48 得分 0

to:   CyberH()  
  try和catch是根据条件选择执行的  
   
  System.out.println("thud()   caught   SubEx");  
                throw   new   IOException();  
  ////////这儿怎么没有抛给下面的System.out.println(   "thud()   caught   IOException");  
  想问一下.throw   new   IOException();算不算条件????Top

4 楼CyberH()回复于 2003-05-03 02:32:07 得分 0

我的意思是说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");  
            }  
        }Top

5 楼wyy_9715072(小宇)回复于 2003-05-03 16:42:53 得分 5

throw相当于return,欧以前也是和   dy15(知足常乐!)相同的看法,不过,自己写了例子测试了一下,发现不是这样的,想想也是,它既然已经return了,怎么还会被下面的捕获呢。收获颇丰  
  祝节日快乐Top

6 楼telenths(_非法操作_)回复于 2003-05-03 16:58:04 得分 7

在一连串   cactch   中   只会有一个   catch   被执行  
  是   catch   的   Exception   与   抛出的   Exception   最接近的那个  
   
  之后就会跳出   try   -   catch   执行   finally   或   下面的内容Top

7 楼leejidongdong(立即动动)回复于 2003-05-03 18:01:12 得分 5

只有一个catch会被进入!Top

8 楼moxie(安子)回复于 2003-05-03 18:06:25 得分 3

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文件目录,呵呵   爽!  
  Top

9 楼moxie(安子)回复于 2003-05-03 18:07:59 得分 0

不好意思,发错了!嘿嘿!Top

10 楼Iforgot(清风雨)回复于 2003-05-03 18:29:14 得分 10

这样的:  
  你抛出SubEx()例外对象,所以你的catch捕获了,但是你的捕获catch   (SubEx   x)的抛出并没有try去捕获。而是被函数抛出了,准备给try他的块去捕获。  
   
  因为你只抛出了SubEx例外,所以,你的catch   (IOException   x)、catch   (Exception   x)当然都没的捕获了。     你的这3个捕获都是对你的try而言的,后面的也不是对你前面的catch用的。  
   
  注意try和catch的配对问题。     建议仔细理解学习一下例外机制。Top

相关问题

  • 关于 try catch
  • Try...catch...finally!
  • try catch 问题
  • try catch finally
  • try{}catch(...)一问
  • try{}catch{}finally{}
  • js的try catch
  • TRY{ } CATCH{ }的作用.
  • try catch 的问题
  • 不会用try catch...............

关键词

  • 执行
  • system
  • 文件
  • 选择
  • subex
  • thud
  • caught
  • ioexception
  • 捕获
  • catch

得分解答快速导航

  • 帖主:dy15
  • CyberH
  • mercury1231
  • wyy_9715072
  • telenths
  • leejidongdong
  • moxie
  • Iforgot

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo