Java中为什么子类重写父类方法时抛出的异常不能大于父类异常

见下方示例代码:


 
 
  1. import java.io.*;
  2. public class Father {
  3. public void father_function() throws IOException {
  4. new File( "a.txt");
  5. }
  6. }
  7. class Son extends Father {
  8. @Override
  9. public void father_function() throws Exception {
  10. new File( "b.txt");
  11. }
  12. }
  13. class Test {
  14. public static void main(String[] args) {
  15. Father fs = new Son();
  16. try {
  17. fs.father_function();
  18. } catch (IOException ie) {
  19. System.out.println( "发生了异常");
  20. }
  21. }
  22. }

代码说明:

  1. 定义了一个类,类名为Father,Father中包含一个普通实例方法,但是该方法会抛出IOException
  2. 定义了一个Father类的子类:Son,该类重写了父类Father的方法,但是子类重写方法后选择了抛出一个更大的异常
  3. 定义了一个Test类,包含一个main方法
  4. Test类中创建了一个Son类对象(也即Father类的子类对象),然后使用try...catch...语句将fs.father_function();这条会抛出异常的语句包围,并使用了IOException对象对当前可能产生的异常进行捕

结果:

         程序编译失败

原因分析:

1、在java多态机制中,对象引用fs在编译时期是属于父类类型也即Father类型,但是在运行时fs属于子类类型,也就是Son类型

2、也就是说在编译的时候,编译器发现catch中的IOException完全能将父类方法中抛出的异常捕获,因此编译通过,但是在运       行时期,由于fs变成了子类类型,子类重写的方法抛出的异常是Exception,显然IOException不能捕获这个比它更大的异           常,因此在运行时期也就出现失败

总结:这个示例也就演示了一个道理,在java中,子类重写父类的方法时,子类如果选择抛出异常,那么抛出的异常类型不能大于父类的异常类型

 

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xichengfengyulou">
                <img src="https://profile.csdnimg.cn/0/1/F/3_xichengfengyulou" class="avatar_pic" username="xichengfengyulou">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/xichengfengyulou" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">西城风雨楼</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog2.png">                                            </div>
                <div class="text"><span>原创文章 6</span><span>获赞 22</span><span>访问量 1万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;,&quot;extend1&quot;:&quot;个人信息未关注&quot;}">关注</a>
                                                            <a href="https://im.csdn.net/im/main.html?userName=xichengfengyulou" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值