Hello,
Just digging up a 12 year old thread on a different list here...
Chuck suggests something like the following:
public void validateForSave() throws ValidationException { NSMutableArray exceptions = new NSMutableArray(); try { super.validateForSave(); } catch (ValidationException e) { exceptions.addObjectsFromArray(e.additionalExceptions()); }
// ... if (exceptions.count() > 0) { throw ValidationException.aggregateExceptionWithExceptions(exceptions); } }
Chuck, does that miss the case where super.validateForSave() just throws a single exception e without nested exceptions? In that case, e.additionalExceptions() will return an empty array, and e doesn't get added to the exceptions array. That seems to be what's happening for me, anyway.
I don't think this would be quite right:
try { super.validateForSave(); } catch (ValidationException e) { exceptions.add(e); exceptions.addObjectsFromArray(e.additionalExceptions()); }
because the Javadocs suggest the container exception duplicates the first exception in the array.
How about this?
try { super.validateForSave(); } catch (ValidationException e) { if (e.additionalExceptions().count() > 0) { exceptions.addObjectsFromArray(e.additionalExceptions()); } else { exceptions.add(e); } }
I'll test it out, but if someone already knows, by all means jump in.
|