自定义异常--继承RuntimeException

1、在实际的项目中,我们需要跟业务相关的异常,javaAPI中是没有的,所以我们要自定义

package cn.tx.array;

public class StockException extends RuntimeException{

public StockException(){

    super();
}
public StockException(String mes ,Throwable cause , 
        boolean  str ,boolean str1 ){

    super(mes , cause , str ,str1) ;
}

public StockException(String mes ,Throwable cause){

    super( mes , cause);
}
public StockException(String mes){

    super( mes );
}

public StockException(Throwable cause){

    super(  cause);
}

}

测试:
package cn.tx.array;

public class ExceptionDemo {

public static void main(String[] args) {

    try {
        submitOrder();
    } catch (Exception e) {
        String mes = e.getMessage();
        System.out.println("提示用户:"+mes);
        // TODO: handle exception
    }
}
public static void submitOrder(){
    int stock = 0;
    if(stock == 0){
        throw new StockException("库存不足");
    }
}

}

你可能感兴趣的:(JAVA基础)