Java day08 异常(2)

来源:互联网 发布:智能马桶圈 知乎 编辑:程序博客网 时间:2024/05/21 19:48
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");elsereturn a/b;}}class RuntimeDemo{public static void main(String[] args) {Div d=new Div();d.div(4,0);}}

0 0
原创粉丝点击