public class M1 extends Exception { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s1 = s.next(); try { if (s1.equals("a")) { throw new M1(); } System.out.println("输入a以外的不会报错"); } catch (M1 e) { System.out.println("输入a则报异常"); e.printStackTrace(); } } }
注意:自定义异常需要继承Exception类