父类代码: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("运行时异常。");
}
}
}
请看官指教!
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("运行时异常。");
}
}
}
请看官指教!
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);
}
}
// 父类的方法为实现整数除法
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
}
}