自定义异常怎么创建与使用

自定义异常创建

//创建一个无参构造函数与有参构造函数

public class X extends Exception{

  public X() {

  }

  public X(String message) {
    super(message);
  }
}

//简单使用自定义异常

public class X1 {

  public static void main(String[] args) throws X{
    try {
      int age = 10;
      if(age >0){
        throw new X("错误!!!");
      }
    } catch (X e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
    }

  }
}

原文地址:https://www.cnblogs.com/xiaosarensheng/p/12674979.html