
编写一个除数为0并声明异常的方法,再由主方法捕获处理。
展开全部
测试可以提交成功不。
package com.baidu;
public class ResultImpl {
public static double getArith(double a, double b)
throws ArithmeticException {
ArithmeticException ex = new ArithmeticException("对不起,除数为0");
if (b == 0) {
throw ex;
}
return a / b;
}
public static void main(String[] args) {
try {
System.out.println(getArith(Double.parseDouble("1"), Double
.parseDouble("0")));
} catch (Exception e) {
System.out.println("做您的处理操作");
e.printStackTrace();
}
}
}
希望可以帮助到您
package com.baidu;
public class ResultImpl {
public static double getArith(double a, double b)
throws ArithmeticException {
ArithmeticException ex = new ArithmeticException("对不起,除数为0");
if (b == 0) {
throw ex;
}
return a / b;
}
public static void main(String[] args) {
try {
System.out.println(getArith(Double.parseDouble("1"), Double
.parseDouble("0")));
} catch (Exception e) {
System.out.println("做您的处理操作");
e.printStackTrace();
}
}
}
希望可以帮助到您
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Du {
public static void main(String[] args) {
double a = 12.3D;
double b = 0D;
try{
double c = divide(a, b);
System.out.println(c);
} catch(ArithmeticException zeroExp){
System.err.println("Divisor can't be ZERO!");
}
}
private static double divide(double a, double b) throws ArithmeticException{
if(b == 0){
throw new ArithmeticException();
}
return a / b;
}
}
JAVA当中,对于除数为0得异常并不主动抛出。对于正数除以0,结果为正无穷大;对于负数除以0,结果为负无穷大;对于0除以0,结果才能显示出出数为0,NaN.
所以只能通过主动判断除数为0来抛出,当然,如果是整数相除,JAVA可以自动判断出除数为0的exception得。
public static void main(String[] args) {
double a = 12.3D;
double b = 0D;
try{
double c = divide(a, b);
System.out.println(c);
} catch(ArithmeticException zeroExp){
System.err.println("Divisor can't be ZERO!");
}
}
private static double divide(double a, double b) throws ArithmeticException{
if(b == 0){
throw new ArithmeticException();
}
return a / b;
}
}
JAVA当中,对于除数为0得异常并不主动抛出。对于正数除以0,结果为正无穷大;对于负数除以0,结果为负无穷大;对于0除以0,结果才能显示出出数为0,NaN.
所以只能通过主动判断除数为0来抛出,当然,如果是整数相除,JAVA可以自动判断出除数为0的exception得。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
除了消化掉没有办法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询