demonexception.java

来自「压缩包内是近180多个针对Java初学者编写的简单实例」· Java 代码 · 共 32 行

JAVA
32
字号
//使用自定义异常类。
class MyException extends Exception {
  private int attribute;
	
  MyException(int i) {
    attribute = i;
  }

  public String toString() {
    return "MyException[" + attribute + "]";
  }
}

class DemonException {
  static void compare(int a) throws MyException {
    System.out.println("Called compare(" + a + ")");
    if(a > 5)
      throw new MyException(a);
    System.out.println("Normal exit");
  }

  public static void main(String args[]) {
    try {
      compare(3);
      compare(15);
    } 
    catch (MyException e) {
      System.out.println("Caught " + e);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?