子类抛出受控异常的限制

package we;

import java.io.*;

public class OverrideThrows
{
    public void test()throws IOException
    {
        FileInputStream fis = new FileInputStream("a.txt");
    }
}
class Sub extends OverrideThrows
{
    //如果test方法声明抛出了比父类方法更大的异常,比如Exception
    //则代码将无法编译……
    public void test() throws FileNotFoundException
    {
            //...
    }
}
一个子类的throws子句抛出的异常,不能是其基类同名方法抛出的异常对象的父类。
posted @ 2019-11-03 21:45  明月照我还  阅读(159)  评论(0编辑  收藏  举报