⑴ java 怎么判断除数是否为零
除数为0了就不该有返回值了,应该手动抛出异常!
publicclassManager{
publicdoublediv(doublex,doubley)
{
try{
if(y==0)
{
thrownewArithmeticException();
}
else
returnx/y;
}catch(ArithmeticExceptionae)
{
ae.printStackTrace();
System.out.println("除数为0");
}
returnx/y;
}
publicstaticvoidmain(Stringar[])
{
Managerm=newManager();
System.out.println(m.div(10,0));
}
}
运行结果: