编写一个运行时异常,并在程序中使用

//编写一个运行时异常,并在程序中使用
public class ExceptionTest {
    public static void main(String[] args) {
        int x=0;
        divide(x);
    }

    private static int  divide(int x) {
        int num=1000;
        if(x==0){
           throw new BadInputException();
        }
        return num/x;
    }
}
public class BadInputException extends RuntimeException{
    //构造方法
    public BadInputException(){
    }
    public BadInputException(String msg){
        super(msg);
    }
}

 

总结:运行时异常,则只需继承于 RuntimeExceptionjava

使用的时候直接:throw new用户自定义异常便可;ide