package com.wuming.exception; //自定义的异常类 public class MyException extends Exception{ //传递数字>10; private int detail; public MyException(int a) { this.detail = a; } //toString 异常的打印信息 @Override public String toString() { return "MyExceptin{" + "detail=" + detail + '}'; } }
package com.wuming.exception; public class Test09 { //可能会存在异常的方法 static void test(int a) throws MyException{ System.out.println("传递参数为:"+a); if(a>10){ throw new MyException(a);//抛出 } System.out.println("ok"); } public static void main(String[] args){ try { // test(11);//传递参数为:11 //MyException==>MyExceptin{detail=11} //说明:抛出异常,不打印ok test(9);//传递参数为:9 //ok } catch (MyException e) { System.out.println("MyException==>"+e); } } }