Y_csgoto 2019-12-05 20:48 采纳率: 60%
浏览 537
已采纳

为什么Java书中实现Java异常链时,在catch{}模块中已经用throw再次抛出了一个异常,却还要在方法定义时throws这个类的异常?

最近学习了Java的异常链机制,发现了一个问题,就是作者实现Java异常链的时候总是在方法定义处throws方法体中已经throw过实例的异常类,显得有些赘余,我想请问这是在干什么,throws和throw不配套使用也不会出错,这样做只是为了什么?以为了后能看得懂?

public class ExceptionList {

    public void firstStep() throws MyException{
        throw new MyException();
    }
    //这里又throws了catch子句里已经抛出的异常
    public void SecondStep() throws AutoDefinedRuntimeException{
        try {
            ExceptionList eList = new ExceptionList();
            eList.firstStep();
        }
        catch(MyException me1) {
            //me1.getMessage();
            me1.printStackTrace();
            throw new AutoDefinedRuntimeException(me1);//抛出更高级的异常
        }
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            ExceptionList eList = new ExceptionList();
            eList.SecondStep();
        }
        catch(AutoDefinedRuntimeException me2) {
            //me2.getMessage();
            me2.printStackTrace();
        }
    }

}

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 Win7电脑开机出现硬件设置已更改
      • ¥15 vba代出现莫须有错误,都是一个个复制下来的为啥出错呢?
      • ¥15 /etc/vsftpd/vsftp.conf配置文件加了一行utf8_filesystem=YES之后,启动vsftpd报错
      • ¥15 W5100可以收广播,但是无法发出广播
      • ¥100 PCD点云排序和分割
      • ¥15 GG-CNN抓取数据集
      • ¥15 C++类和对象,多态性,继承,虚函数虚基类
      • ¥15 使用VS2019和Dev c++按%p输出地址,结果相差很大
      • ¥30 有偿解惑TINA老报错,求解惑
      • ¥15 arduino esp8266 编译问题