Java------Exception Handling

Source: Internet
Author: User


Import Java.util.*;import java.text.*;class chinesedate{private Calendar aday; public chinesedate (int, Int. Month,int day) throws mydateexception{//this.aday= new Calendar ();//No, because Calendar is abstract class/  /this (); This.aday = Calendar.getinstance ();  ※※※this.set (Year,month,day);  } public Chinesedate () {this.aday = Calendar.getinstance (); } public void Set (int year, int month, Int. day) throws mydateexception{//throw exception//handle the legality of business logic if (year<=0 | | year>250 0) {throw new Mydateexception ("Year error, valid year: 0~2500.  ");  } if (Month<=0 | | month>12) {throw new Mydateexception ("Month error!");  } if (Day<=0 | | day>daysofmonth (year,month)) {throw new Mydateexception ("date error"); } this.aday.set (Year,month-1,day);     ※※※} public static Boolean isleapyear (int year) {Boolean boo=year%400==0 | | year%100!=0&&year%4==0;   return boo;   } public boolean isleapyear () {return this.isleapyear (Aday.get (calendar.year)); } public static int daysofmonth (int year,inT month) {switch (month) {case 1:case 3:case 5:case 7:case 8:case 10:case 12:return 31;       Case 4:case 6:case 9:case 11:return 30; Case 2:return isleapyear (year)?       29:28;     Default:return 0;   }}/*//※※※public int daysofmonth () {return Daysofmonth (Aday.get (calendar.year), Aday.get (Calendar.month)); }/Public String toString () {return new SimpleDateFormat ("yyyy mm month DD Day eeeee a hh mm min ss sec"). Format (The new Date (AD  Ay.gettimeinmillis ()));  } public static void Main (String args[]) {try{//exception, exception handling System.out.println (new Chinesedate (2015,1,23));  }catch (mydateexception e) {System.out.println ("AAAA----:" +e.tostring ()); if (e.tostring (). Equals ("Year error, valid year: 0~2500.  ") {System.out.println (" the exception that was found to be faulted for the year, to be processed accordingly .... "); } if (E.tostring (). Equals ("Month error!"))  {System.out.println ("the exception for the discovery of month error, to be processed accordingly ....");  } if (E.tostring () equals ("Date error")) {System.out.println ("Exception found on date error, process accordingly ...."); }}catch (Exception e) {E.printstackTrace ();    } System.out.println ("I'm fine ..."); }}







Java------Exception Handling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.