[3151] in java-interest
Re: I want to shoot myself!!!!!!!!!!!!!!!!!!
daemon@ATHENA.MIT.EDU (Jonathan Payne)
Wed Nov 1 19:20:46 1995
Date: Wed, 1 Nov 1995 12:18:22 -0800
From: jpayne@starwave.com (Jonathan Payne)
To: Thomas.Ball@Eng.Sun.COM
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <9511011922.AA22674@alapa.Eng.Sun.COM> (Thomas.Ball@Eng.Sun.COM)
> Date: Wed, 1 Nov 1995 11:22:41 -0800
> From: Thomas.Ball@Eng.Sun.COM (Thomas Ball)
> Cc: java-interest@java.Eng.Sun.COM
> X-Sun-Charset: US-ASCII
> Content-Type: text
> Content-Length: 3336
>
> Basically, there are two groups of errors: those you can reasonably
> recover from and those you can't.
That's it! That's what I was looking for! Just those simple words.
If I pass some bogus data to a routine, because I am an idiot, then
that's not something I can really recover from. I must fix the bug.
RuntimeExceptions are for those kinds of errors, bugs, that is, and
Exceptions are for ones that you can reasonable recover from.
Now with this in mind, let me think of how many exceptions would I
require declaring and how many would I not?
OK, I have a regular expression package. MalformedRegexpException is
thrown when you try compiling an expression that is bogus. This fits
into the category of not-recoverable. It was a programming error of
mine which caused me to generate a bogus regexp string.
Also, if you request a particular matched region of a regular
expression, it is an error if you request a match that doesn't exist.
That's also a programming error, not really recoverable from except by
fixing your code. So that's also a RuntimeException.
Hmmm, RuntimeException 2, Exception 0.
OK, how about adding a record to a database. I call a method to set a
column in a row:
record.setColumn("time", new Date());
record.setColumn("name", new Date());
The first one works, the second one fails because the "name" column is
not supposed to take a Date as an argument. Is this a
RuntimeException? Yes, it's just like a ClassCastException, in that
you expected a String and you got a Date, and the compiler couldn't
check for you because you have to name your fields as Strings.
Hmmm, RuntimeException 3, Exception 0.
But say you lose your connection to the database because it goes down
on you. So you might get a ConnectionLostException. That's something
you can recover from.
Hmmm, RuntimeException 3, Exception 1.
> Date: Wed, 1 Nov 1995 12:06:15 -0800
> From: Arthur.Vanhoff@Eng.Sun.COM (Arthur van Hoff)
>
> Hi Jon,
>
> Cheer up!
Feeling better now ...
> RuntineExceptions should be used only for things that can happen anywhere
> regardless of what method you are calling. An example is NullPointerException.
> It would be silly if you had to declare it everywhere because it can happen
> anywhere.
What about ArrayIndexOutOfBoundsException? That can only happen if
you happen to be accessing an array. StringIndexOutOfBoundsException
happens only when you are accessing a String.
I like Tom's approach. If it something you can reasonably recover
from, make it an Exception that you must declare. If it's really a
programming error, then make it a RuntimeException. There's nothing
you can do in that case except fix your code. It doesn't mean you
can't catch it - it just means it's not part of the API.
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com