创建一个自义定的异常类,继承

img

class MyException extends Exception{
  String reason;

  public MyException(String reason) {
    this.reason = reason;
  }

  @Override
  public String toString() {

    return "MyException [reason=" + reason + "]";
  }

  
}

public class Main {
  public static void main(String[] args) {
    try {
      throw new MyException("test1");
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}