package Exception; public class AgeException extends Exception{ public AgeException(String message) { super(message); } }
package Exception; public class Demo04 { private int age; public int getAge() { return age; } public void setAge(int age) throws AgeException{ if(age>=1&&age<=100) { this.age = age; }else { throw new AgeException("年龄必须在1到100之间!"); } } public static void main(String[] args) { Demo04 d = new Demo04(); try { d.setAge(194); } catch (AgeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }