父类代码:class B {
    void athrow(int x, int y) throws ArithmeticException {
        return x / y;
    }
}
子类代码:public class B extends A {
    void athrow() /*这里要加throws 异常吗?*/ {
        try {
            return x / y;
        } catch (ArithmeticException e) {
              System.out.println("运行时异常。");
          }
    }
}
请看官指教!

解决方案 »

  1.   

    子类忘记写main函数了,这里补下:
    public class B extends A {
        void athrow() /*这里要加throws 异常吗?*/ {
            try {
                return x / y;
            } catch (ArithmeticException e) {
                  System.out.println("运行时异常。");
              }
        }    public static void main (String[] args) {
            B baby = new B();
            B.athrow (3/0);
        }
    }
      

  2.   

    当重写父类方法,子类在抛出异常声明方面,要满足下面任意一条:1. 不声明抛出异常。父类函数中,已经声明过了,可以不声明。2. 子类声明的异常必须是父类方法声明那种异常或者其子类的异常。例如:父类声明过ArithmeticException,子类声明只能为ArithmeticException或者ArithmeticException的子类的异常。例如:父类声明Exception,这是所有异常类的父类,子类可以声明ArithmeticException。自己编程实现了这个疑问了。自己应该多思考,多动手,而不是什么问题都来问人。
      

  3.   

    遇到这种问题时,用 ECLIPSE 试一下就知道了
      

  4.   

    父类代码:public class A {
    // 父类的方法为实现整数除法
    int athrow(int x, int y) throws ArithmeticException {
    return x / y; //当除数为0,会产生ArithmeticException异常
    }
    }
    子类代码:public class B extends A {
    int athrow(int x, int y) /* throws ArithmeticException */ {
    try {
    return x/y;
    } catch (ArithmeticException e) {
    System.out.println("异常为:" +e);
    }
    return 1;
    }

    public static void main (String[] args) {
    B baby = new B(); baby.athrow(3, 0); //这里会产生异常,因为输入的值,除数为0
    }
    }