日期的`異常`捕獲

 /**
 *  日期的異常捕獲
 */
//年異常
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;
        }
    }
    //人類的構(gòu)造函數(shù),因?yàn)樯婕吧盏某跏蓟?可能發(fā)生異常
    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());
        }       
    }
}

結(jié)果:

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 !
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蕾哟,一起剝皮案震驚了整個(gè)濱河市综液,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌朦促,老刑警劉巖童太,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件米辐,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡书释,警方通過(guò)查閱死者的電腦和手機(jī)翘贮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)爆惧,“玉大人狸页,你說(shuō)我怎么就攤上這事〕对伲” “怎么了芍耘?”我有些...
    開封第一講書人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)熄阻。 經(jīng)常有香客問(wèn)我斋竞,道長(zhǎng),這世上最難降的妖魔是什么秃殉? 我笑而不...
    開封第一講書人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任坝初,我火速辦了婚禮,結(jié)果婚禮上钾军,老公的妹妹穿的比我還像新娘鳄袍。我一直安慰自己,他們只是感情好吏恭,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開白布拗小。 她就那樣靜靜地躺著,像睡著了一般樱哼。 火紅的嫁衣襯著肌膚如雪哀九。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,246評(píng)論 1 308
  • 那天唇礁,我揣著相機(jī)與錄音勾栗,去河邊找鬼。 笑死盏筐,一個(gè)胖子當(dāng)著我的面吹牛围俘,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼界牡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼簿寂!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起宿亡,我...
    開封第一講書人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤常遂,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后挽荠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體克胳,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年圈匆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了漠另。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡跃赚,死狀恐怖笆搓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情纬傲,我是刑警寧澤满败,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布,位于F島的核電站叹括,受9級(jí)特大地震影響算墨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜汁雷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一米同、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摔竿,春花似錦、人聲如沸少孝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)稍走。三九已至袁翁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間婿脸,已是汗流浹背粱胜。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留狐树,地道東北人焙压。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親涯曲。 傳聞我的和親對(duì)象是個(gè)殘疾皇子野哭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容