Lew wrote
No, quite the contrary. And so what if you did?
Surely one of the benefits of newsgroups is that they allow other people
to look at archived questions and learn from them. If the archived
materials are severely trimmed, that could make it much harder to learn
from.
The year 1 never happened either. Neither did the year 100. Are you
going to reject those years, too?
Again, I don't particularly want to dwell on the year 0 here. I
introduced this merely as an example of something that might be a
candidate for a checked exception.
I don't claim to be a guru on the history of time but I am interested in
history, although mostly contemporary history rather than the Roman era.
If the year 0 did exist and the years 1 and 100 didn't, this is the first
I'm hearing of it. No history course I ever took at high school or
university suggested any such thing. The teachers I had talked about B.C.
and A.D. and said that 1 A.D followed 1 B.C. and that there was no year
0. Umpteen timelines all followed that same model.
[Now, I recognize that a lot of this is "retroactive" in the sense that
the Romans didn't start distinguishing between B.C and A.D in the year I
would call 1 A.D. Western historians started doing that well after the
fact. Perhaps that's what you mean when you say the year 0 did exist and
the years 1 and 100 did not??]
Note, again, that the standard Java API does accept the year 0,
because it uses the proleptic Gregorian calendar.
Are you going to reject October 10, 1582? That only happened some
places. Are you going to consider February 28, 1560, to be in the
same year as October 28, 1560? That would be wrong. Do you consider
that Cervantes and Shakespeare died on the same day because they died
That's because you haven't studied calendars very much, and apparently
haven't bothered to read the Javadocs very much either.
For pity sake, I DID read the Javadocs, although I didn't necessarily
understand everything I saw. I am now going to assume that I've said that
enough times that I don't need to say it again.
Well, they didn't happen in the Gregorian calendar. Why wouldn't you
reject them?
I am not clear on what a Gregorian calendar does with dates before the
establishment of the Gregorian calendar in 1582 (for the Catholic
countries and later for other countries.)
I am not trying to become an expert in the history of timekeeping. I'm
trying to ask a question about checked and unchecked exceptions. The
example that I thought would represent a simple illustration of a
validation in a method has obviously backfired on me. So let me just
contruct a new example that will, I hope, be less controversial so that I
can actually confirm my understanding of exceptions.
And am I not permitted to introduce a new concept, or must everything
I suggest have been brought up already before I comment?
I said nothing of the kind. I don't know where you are getting that.
Then you already knew about the proleptic Gregorian calendar and that
the Java 'GregorianCalendar' accepts year 0.
Well, I pointed out that "recoverable" was not relevant, as it is far
too loose a concept that can apply to both checked and unchecked
exceptions, and therefore does not distinguish when to use one or the
other.
And that's why I tried to put the concept in somewhat more practical
terms.
No, it isn't.
That reasoning applies equally well to checked or unchecked
exceptions.
Then I am still not clear on when I would use a checked exception and
when I would not.
I do NOT want to open this Year 0 can of worms again so let's try
something that will hopefully be less controversial.
I have a method called getSeconds. Its job is simply to take a number of
hours, minutes and seconds and convert that to seconds. Here is the code,
minus the comments (for brevity):
public int getSeconds(int hours, int minutes, int seconds) {
if (hours < 0) {
Object[] msgArgs = {new Integer(hours)};
msgFmt.applyPattern(locMsg.getString("msg032"));
throw new IllegalArgumentException(msgFmt.format(msgArgs));
}
if (minutes < 0) {
Object[] msgArgs = {new Integer(minutes)};
msgFmt.applyPattern(locMsg.getString("msg033"));
throw new IllegalArgumentException(msgFmt.format(msgArgs));
}
if (seconds < 0) {
Object[] msgArgs = {new Integer(seconds)};
msgFmt.applyPattern(locMsg.getString("msg034"));
throw new IllegalArgumentException(msgFmt.format(msgArgs));
}
return (hours * HoursMinutesSeconds.SECONDS_IN_ONE_HOUR)
+ (minutes * HoursMinutesSeconds.SECONDS_IN_ONE_MINUTE)
+ seconds;
}
Can we agree that it makes no sense to have negative hours, minutes and
seconds and that if the calling program supplies negative numbers for any
of those parameters, it is reasonable to notify the caller of this fact
so that the caller can, perhaps, remedy the situation?
Would we agree that this example illustrates a proper use of unchecked
exceptions?
As I have said a couple of times already in this thread, and surely
you have read, because it accomplishes nothing. It does not add to
the documentation, it does not become part of the method signature, it
does not require callers to catch the exception, and it's a waste of
time and effort. The 'throws' clause is designed for and only useful
with checked exceptions.
Okay, fine.
But that is correct behavior, by ISO standard 8601. You really need
to study up on calendars, particularly the Gregorian calendar. May I
suggest careful study of
<
http://en.wikipedia.org/wiki/Gregorian_calendar>
particularly
<
http://en.wikipedia.org/wiki/
Gregorian_calendar#Proleptic_Gregorian_calendar>
?
Agreed, my ignorance of calendars and ISO 8601 is overwhelming. I will
remedy that ignorance when time permits.
Sometimes people, and APIs, do the right thing even if you don't tell
them to. Furthermore, it is not your job to tell the standard API
what to do, but the standard API's job to tell you what it actually
does. As long as it works as documented, then you have no legitimate
gripe.
"Recoverable" is an awful, terrible, vague, useless, unworthy and
ridiculous criterion for choosing between the two. Instead, consider
either
- RuntimeException for programmer error, checked exception for
environmental error, or
- RuntimeException to let a caller slide without a 'catch' clause,
checked exception to force the 'catch'.
Whatever criterion you use, it's going to be a matter of style. There
are entire APIs out there that refuse to throw checked exceptions by
dint of the API writers' philosophy, others that go hog-wild with
them. It's up to the API writer what they choose to impose.
Fair enough.