62,614
社区成员
发帖
与我相关
我的任务
分享
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("运行时异常。");
}
}
}
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
}
}
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);
}
}