Paul Clapham wrote:
Bd Howard wrote:I am sure there are instances where you would have to handle it locally and others where you'd pass it up the tree.
Yes, this is correct. You always have to make this choice when you're catching an exception. And as you suggest, the answer always depends on the design of the application.
The general rule though is "throw early, catch late." Most of the time, the code where the exceptions occur isn't in a position to decide what to do about, so it just informs the caller, either by not catching the exception in the first place, or by catching, wrapping in something more "layer-appropriate", and throwing that.
Sometimes it may be able to retry (for instance if it's code that you know should have no reason to fail the vast majority of the time, but may occasionally due to timing issues) or provide some default value (if the value it was supposed to produce was optimal but not necessary, and getting
some value, even a sub-optimal one, is better than failing). In my experience, however, those cases are rare.