Let’s say you have a function (X) that takes a single object and does some work with it. It can fail in some way:
function X(obj) throws SomeException
And I have a collection of these objects, and want to run X on all of them. So I wrap that up in its own function:
function Y(objs)
foreach obj in objs
X(obj)
end
end
What is the best way of handling the exceptions returned by X?
One obvious choice would be to not catch the exception at all, causing the processing of the collection to stop at the first failure.
Another would be to catch each exception as it occurs and save it aside. At the end, you could then throw a new exception if there were any exceptions at all. You could optionally choose to include these exceptions as nested exceptions of the new exception.
Another would be to ignore the exception completely, returning something like a boolean array that marks success/failure, or even returning the array of exceptions that occurred.
It depends very much on the contxt of what you are doing.
If every item must be processed then breaking at the first exception would may be the best option.
If you are committing a bunch of items which have been parsed from a third party source and you are expecting some failures keeping a note of the failed items and returning a status object may well be the way to go.