日期的`異常`捕獲

 /**
 *  日期的異常捕獲
 */
//年異常
class YearException extends Exception{
    String info;
    int year;
    public YearException(){}
    public YearException(String info){
        this.info=info;
    }
    public YearException(String info,int year){
        this(info);
        this.year=year;
    }
    public String getInfo(){
        return this.info;
    }

}
//月異常
class MonthException extends Exception{
    String info;
    int month;
    public MonthException(){}
    public MonthException(String info){
        this.info=info;
    }
    public MonthException(String info,int month){
        this(info);
        this.month=month;
    }
    public String getInfo(){
        return this.info;
    }

}
//日異常
class DayException extends Exception{
    String info;
    int day;
    public DayException(){}
    public DayException(String info){
        this.info=info;
    }
    public DayException(String info,int day){
        this(info);
        this.day=day;
    }
    public String getInfo(){
        return this.info;
    }

}
//日期異常,如閏年2月29
class DateException extends Exception{
    String info;
    int year;
    int month;
    int day;
    public DateException(){}
    public DateException(String info){
        this.info=info;
    }
    public DateException(String info,int year){
        this(info);
        this.year=year;
    }
    public DateException(String info,int year,int month){
        this(info,year);
        this.month=month;
    }
    public DateException(String info,int year,int month,int day){
        this(info,year,month);
        this.day=day;
    }
    public String getInfo(){
        return this.info;
    }

}
//人類
class Person{
    BirthDay birthday; //生日
    class BirthDay{
        int year;
        int month;
        int day;
        public BirthDay(int year,int month,int day){

            this.year=year;
            this.month=month;
            this.day=day;
        }
    }
    //人類的構造函數,因為涉及生日的初始化,可能發生異常
    public Person(int year,int month,int day)throws YearException,MonthException,DayException,DateException{
        if(year<0){
            throw new YearException("Exception: Year = "+year+" < 0 !",year);
        }else if(month<1){
            throw new MonthException("Exception: Month = "+month+" < 1 !",month);
        }else if(month>12){
            throw new MonthException("Exception: Month = "+month+" > 12 !",month);
        }else if(day<0){
            throw new DayException("Exception: Day = "+day+" < 0 !",day);
        }else if(day>31) {
            throw new DayException("Exception: Day = "+day+" > 31 !",day);
        }else if((month==4||month==6||month==9||month==11)&& day > 30 ){
            throw new DateException("Exception: Month = "+month+",but Day = "+day+"> 30 !",year,month,day);
        }else if(month==2&&day>29) {
            throw new DateException("Exception: Month = "+month+",but Day = "+day+"> 29 !",year,month,day);
        }else if(!((year % 4 == 0 && year % 100 != 0) ||year%400==0)&&day>28){
            throw new DateException("Exception: Year = "+year+" is not Leap Year,"+" Month = "+month+",but Day = "+day+" > 29 !",year,month,day);
        }else{
            this.birthday = new BirthDay(year,month,day);
        }
        
    }
}

class DateDemo{
    //入口
    public static void main(String[] args){
        try{
            //年異常
            Person p1 = new Person(-1994,9,12);
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        
        try{
            //月異常
            Person p2 = new Person(1994,19,12);   
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        
        try{
            //日異常
            Person p3 = new Person(1994,9,32);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        try{
            //二月異常
            Person p4 = new Person(1994,2,30);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }
        try{
            //閏年二月異常
            Person p5 = new Person(2017,2,29);    
        }
        catch(YearException e){
            System.out.println(e.getInfo());
        }
        catch(MonthException e){
            System.out.println(e.getInfo());
        }
        catch(DayException e){
            System.out.println(e.getInfo());
        }
        catch(DateException e){
            System.out.println(e.getInfo());
        }       
    }
}

結果:

MacbookPro:JAVA Hx$ java DateDemo
Exception: Year = -1994 < 0 !
Exception: Month = 19 > 12 !
Exception: Day = 32 > 31 !
Exception: Month = 2,but Day = 30> 29 !
Exception: Year = 2016 is Leap Year, Month = 2,but Day = 29 > 28 !
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容