12
返回列表 发新帖
楼主: zhuzhichao

谁能告诉我这样的代码,编译为何不会报错?

[复制链接]
论坛徽章:
4
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33
11#
发表于 2002-1-29 11:06 | 只看该作者
最初由 liu_linux 发布
[B]子类重定义父类的方法,应该是一个新的方法,两个方法的异常没有联系。 [/B]


对于方法的覆盖,是有关系的,zhuzhichao说的很对的
对于方法的重载,是没有关系的

使用道具 举报

回复
论坛徽章:
0
12#
发表于 2002-1-29 19:43 | 只看该作者

火龙骑士的代码里不是方法的覆盖吗?

来自JAVA原版教材对OVERRDING 方法中异常的处理
The declared exception class must be the same class or a subclass.
For example, if the superclass method throws an IOException,
then the overriding method can throw an IOException, a
FileNotFoundException (a subclass of IOException), but not an
Exception (the superclass of IOException)
l Fewer exceptions may be declared in the throws clause.
l New exceptions may not be added to the throws clause.
但问题提出的是明明是方法的覆盖,而不是重载,很明显是在两个类里面的方法,不会是重载吧?

使用道具 举报

回复
论坛徽章:
1
ITPUB元老
日期:2005-02-28 12:57:00
13#
发表于 2002-1-31 15:07 | 只看该作者
//agree
其实SCJP的教材里已经说的很清楚了。定义throw runtimeexception等于是没有定义。不会起作用的。不过教材再overriding这部分确实没有单独提到队runtime exceptin怎么处理的。

使用道具 举报

回复
论坛徽章:
1
ITPUB元老
日期:2005-02-28 12:57:00
14#
发表于 2002-1-31 15:09 | 只看该作者
最初由 zhuzhichao 发布
[B]就是說子類定義了RuntimeException

就等於沒寫,沒定義任何異常? [/B]

是的。

使用道具 举报

回复
论坛徽章:
3
ITPUB元老
日期:2005-02-28 12:57:00授权会员
日期:2005-10-30 17:05:33会员2006贡献徽章
日期:2006-04-17 13:46:34
15#
发表于 2002-1-31 20:46 | 只看该作者

你们好像没有理解异常的具体意义

java书里说的好像挺清楚,是checked exception,在父类中没有定义的话,子类将不能抛出,记住,是checked exception,不是exception,更不是error,对于runtimeexception,你可以查看它的文档:
RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表