What do you base this feeling on?
by the way. thanks for the corrections which you made to many of the
things I suggested in the previous post.
a)Integer.parseInt was one of the method that got up my nose. It
throws a NumberFormatException - which is, checked.
b) BufferedReader br = new ..........FileReader(....)
that'll throw a FileNotFoundException (which is checked)
and
c) - related to b
br.read(...) <-- that'll throw an IOException (which si checked)
In the case of 'a' - integer.parseInt throwing the checked exception
Regarding the Integer.parseInt example. A method that returns a
boolean, could discern whether it is an integer.
Then one could call a method like Integer.parseInt which would throw an
IllegalArgumentException if it was passed a String that wasn't
representing an Integer .
String tbox="12345"
if(testint(tbox)) Integer.parseInt(tbox); else
System.out.println("Enter an integer");
Regarding FileReader throwing a FileNotFoundException. Again, I can
test if the file exists, and so, if I were to call FileReader, passing
it a file that didn't exist, then I would want an
IllegalArgumentException thrown. It's an error I could avoid and I
wouldn't write code to recover from it since it'd be programmer error.
Fine, in a multiuser application - many threads, some user may delete
the file after I verify that it exists. But since i'm writing a single
user / single thread, application, that sort of mischief isn't
expected, and I rightly don't want to check and recover from that. Let
thep program crash if the moment after I test for the file's existance,
the file disappears!.
If it were an unchecked exception, i'd have the choice to catch it if I
wanted to deal with it. But here - it beign a checked exception - I am
forced to catch it or declare it in the throws
Regardign c - br.read(..) . that I can understand, if it's a big
file, maybe the file will disappear. But still, I shouldn't be forced
to recover from that. Suppose I'm reading a small file, it'll take less
than 5 seconds to read. If the file suddenly disappears, then it's
blatant user negligence, and I don't want to code for that possibility,
I'm happy for the program to crash.
I think, your solution may be the only one. To catch the specific
checked Exceptions and throw a Runtime Exception (which is unchecked).
It's still longwinded though. And those methods I mention are quite
common.
I guess that since they are common, one could rewrite versions of those
methods that don't throw checked exceptions.
Anyway, if you do think that the method should throw an unchecked
exception, wrap it.
public static void sleep(final int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (final InterruptedException exception)
{
throw new RuntimeException(exception);
}
}
I personally don't ever specify unchecked exceptions in throw clauses,
because that's code that has no effect.
No, you are adapting the throws clause. If you say that you throw
Exception, then you are forcing the callers of your method to do the
same, or catch Exception. This is pointless.
yeah, it was silly. That all encompassing Exception was of course a
Checked Exception.
Unless you actually do throw instances of Exception, not subclasses,
which itself would be pointless, you should declare to throw all
checked exceptions. If you don't want checked exceptions in your
signatures at all, then Exception is no better. Here's my usual
solution (really the previous one repeated):
public void doSomething()
{
try
{
code
}
catch (final IOException exception)
{
throw new RuntimeException(exception);
}
catch (final SQLException exception)
{
throw new RuntimeException(exception);
}
}
If you force people to catch Exception, you are forcing them to catch
RuntimeException too, which is usually not desired. I would ALWAYS
have separate catch blocks for each checked exception type, and a
separate one for RuntimeException. CheckStyle and findbugs have a
little to say on the matter, and can point out this error in your code:
http://findbugs.sourceforge.net/bugDescriptions.html#REC_CATCH_EXCEPTION
http://checkstyle.sourceforge.net/config_coding.html#IllegalCatch
yikes, so, I won't be doing that catch exception adn throws exception!
How about this, which rethrows unchecked exceptions like NPEs and IAE
try
{
/* code with Integr.parseInt, FileReader, .read(..) */
}
catch(Exception e)
{
/*
Have a method that tests the Exception, if it is an
IOException or a
FileNotFoundException, then the method will catch it -
swallowing it.
Otherwise, it's some other exception, like a unchecked IAE
or NPE.
So it'll throw that Exception back out.
*/
}