package cn.exp; //自定义异常继承自Exception //注意throw和throw的区别 class ExceTest extends Exception { private String errorMessage; public ExceTest(String errorMessage) { this.errorMessage = errorMessage; } public String showMessage() { return errorMessage; } } class TestClass {//注意throw和throw的区别 public void show(int a, int b) throws ExceTest { if (b < 0) throw new ExceTest("参数错误"); System.out.println("参数正确;若出错,此句便不会输出"); } } public class ExceptionDemo1 { public static void main(String[] args){ TestClass testClass=new TestClass(); try { testClass.show(3, -4); } catch (ExceTest e) { System.out.println(e.showMessage()); } } }