0219作业02

 1 package kslx.zuoye.zyoye021902;
 2 /**
 3  * 测试类
 4  */
 5 public class ExceptionTest {
 6     public static void main(String[] args){
 7         Zuoye02 zy=new Zuoye02();
 8         try{
 9             zy.setAge(111);
10         }catch (AgeException e){
11             e.printStackTrace();
12         }
13     }
14 }

2

1 package kslx.zuoye.zyoye021902;
2 /**
3  * 自定义年龄异常
4  */
5 public class AgeException extends Exception{
6     public AgeException(String a){
7         super(a);
8     }
9 }

3

 1 package kslx.zuoye.zyoye021902;
 2 
 3 /**
 4  * 年龄类
 5  */
 6 public class Zuoye02 {
 7     private int age;
 8 
 9     public Zuoye02() {
10     }
11     public Zuoye02(int age) {
12         this.age = age;
13     }
14 
15     public int getAge() {
16         return age;
17     }
18 
19     public void setAge(int age) throws AgeException{
20         if(age>0 && age<=100){
21             this.age = age;
22         }else{
23             throw new AgeException("年龄必须1-100之间");
24         }
25 
26     }
27 }

4运行结果

 

posted @ 2019-02-20 14:30  纯属丶简单  阅读(142)  评论(0编辑  收藏  举报