Re: When to throw exceptions and when to use System.err?
Eric wrote:
> Lew wrote:
YES! You absolutely MUST track down EVERY possible reason for error and prevent it.
THAT'S PROGRAMMING!
Why would you ever do otherwise? Do you actually want to leave possibility for error in your program?
Really? Why?
Again, don't catch exceptions, prevent them. Do you recall that I posted upthread:
<http://java.sun.com/docs/books/effective/toc.html>
What part of that answer was not clear?
The part where you said all possible errors should be caught and every
method should exit normally passing out values for every possible
error not throwing anything, then you say it's good to throw errors
because if it were not good practice then the Java API would not do
it, the java.io.File.read() would not crash if the file were
unavailable or the program didn't have access to it, it would exit
gracefully passing out a value to let your program know exactly what
the problem is.
OK, please allow me to clarify, and correct any erroneous comment I made.
One can throw an exception from a method, of course. I didn't say that was
bad. I did say one should not let an exception crash a *program*.
To wit:
Catch it, log it, and restore the program to a sane state.
Sometimes that means rethrowing the exception, but very rarely. More common
is to recast into an application catch-all exception. More common, or better
practice in more situations, is to provide an alternate return from the
operation to signal to the caller that there was a problem - that's where a
buried exception can be rethrown sometimes. It's a matter of art.
Note:
- "restore the *program* to a sane state" - not the method
- I did endorse throwing exceptions from methods
- I described several options
- I made no absolute statements other than EVERY exception must be handled -
again, I'm not saying don't rethrow or wrap-and-throw, but then /that/
exception must be handled so that the *program* gets unexceptional.
Is that clearer?
--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg