Say you normally have FooException
.
/**
* @throw FooException If Foo is invalid for searching.
*/
public bool exists(Foo a)
But at some point you need to have two more specific exceptions for Foo.
/**
* @throw FooSearchException (extends FooException) When "a" caused the error.
* @throw FooReplacementException (extends FooException) When "b" caused the error.
*/
public int replace(Foo a, Foo b)
Because you want be able to know which of the two parameters caused the error.
At this point, what should I do with the exists()
method? Should I change it to throw the most specific Exception type or should I keep it generalized as much as possible to uniquely identify a problem in a single routine? The exists()
method doesn't need to differentiate between Search
and Replacement
parameters, but it is still about a Search
. What should I do?
(Note that the change won't affect existing catch()
es since both FooSearchException
and FooReplacementException
extend FooException
)