您的位置:首页 > 编程语言 > Java开发

Java day08 异常(2)

2016-03-22 22:29 465 查看
class NegativeException extends ArithmeticException
{
NegativeException(String msg)
{
super(msg);
}
}
//若在方法体中抛出非RuntimeException,而在方法上没有声明该异常,则发生编译错误
class Div
{
int div(int a,int b)
{
if(b<0)
throw new NegativeException("除数是负数");
else if(b==0)
throw new RuntimeException("除以了0");
else
return a/b;
}
}
class RuntimeDemo
{
public static void main(String[] args)
{
Div d=new Div();
d.div(4,0);

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: