28 July 2010

My favorite Exception

/* Create calendar date of the time */
GregorianCalendar cal = new GregorianCalendar();

if ( (month == 9 && day > 30)
    || (month == 4 && day > 30)
    || (month == 6 && day > 30)
    || (month == 11 && day > 30)
    || (day < 0 || day > 31)
    || (month == 2 && !cal.isLeapYear(year) && day > 28)
    || (month == 2 && cal.isLeapYear(year) && day >29)
)
{
    String rhyme = "30 days hath September,"
        + "\nApril, June and November,"
        + "\nAll the rest have 31,"
        + "\nExcepting February alone"
        + "\n(And that has 28 days clear,"
        + "\nWith 29 in each leap year).";

    throw new Exception(rhyme);
}


:D

I'm going to use that a lot now, because it just makes me laugh, and no one likes boring exceptions.

No comments: