Re: Understanding Exceptions
On 11/10/2010 4:28 AM, Steve Crook wrote:
On Tue, 09 Nov 2010 19:26:29 -0800, markspace wrote in
Message-Id:<ibd3d5$9kl$1@news.eternal-september.org>:
I don't like this solution above. Why? Why inform the calling code
that you might not find the algorithm you were looking for? They did
not pass the name of the algorithm in as a parameter. There's nothing
the caller could do about it. There's a maxim, I believe, about only
throwing exceptions when the caller could take some action to rectify
the situation.
I agree with this if, and only if, "rectify the situation" includes
making decisions about program termination and error reporting, and
"caller" includes the caller's caller etc.
I think it depends on what scope of action the calling code can take
when the exception occurs. As this is an application specific problem,
I don't think there is a correct solution for all possibilities.
To my mind, the fact that the correct solution is application specific
is the strongest argument in favor of throwing an exception.
The method in question, rightly, does not even know whether it is
running in a batch program or a GUI, and has no idea whether its failure
should be considered a critical failure of the whole program, or a minor
glitch. It is a nice little reusable method that does one simple job,
and that job cannot be done because of the unavailability of the
algorithm it needs.
Somewhere up the stack, there are methods that know more of the context.
For example, some method decided to call the method that failed. It
knows more than that method does about why it was called, and the
consequences of its failure. At the other end of the stack, there are
methods that know whether the program is in GUI communication with a
user who would expect a dialog box rather than an exit.
I view exceptions as a way of passing the information about what went
wrong up the stack, with intermediate methods possibly adding
information, until it reaches a method that does knows enough to deal
with it definitively.
A method that calls System.exit for a failure should only be used in a
program if its operation is so critical to the program that its failure
should crash the program. A method that logs its failures should only be
used in programs that have logging enabled. A method that throws an
exception can be used in any program.
Patricia