in OO programming, is there some conceptual pattern, ideas, about handling multiple errors?
for example, i have a method that performs some checks and should return an error message for each error found
[‘name is too short’, ‘name contains invalid unicode sequences’, ‘name is too long’]
now, should i use an array of exceptions (not thrown exceptions)?
or something like this is better:
class MyExceptionList extends Exception{
public Void addException(Exception e){}
public Array getExceptions(){}
}
any theory behind this argument will be appreciated!
(this isn’t a request about a specific programming language, but a pure theoretical one)
thank you in advance
Unfortunately, many languages and frameworks (including C++, Java, and .net) use an exception-handling mechanism which requires the type of an an exception object to simultaneously answer many questions, including:
Unfortunately, while the answers to those questions are somewhat related, they are in reality far from 100% correlated. Unfortunately, the assumption that the type of an exception will be sufficient to answer all of those questions makes it difficult to deal sensibly with many situations.
If you have control over all the exceptions that can be thrown, it may be helpful to use an exception-handling paradigm where the exception-handling object includes a virtual
IsResolvedproperty or method along with aShouldCatchAs<T>property or method that returns aTif the exception needs to be handled as aT. Such a paradigm would be able to smoothly handle situations where an exception occurs while unwinding the stack from an earlier exception (the existing exception and new one would be wrapped into a composite exception object, whoseShouldCatchAsproperty would combine those of the original exceptions, and whoseIsResolvedproperty should only returntruewhen both of the original exceptions’IsResolvedproperties do likewise).I don’t know any way to integrate such behavior into the existing frameworks unless one catches and wraps all exceptions that don’t fit the paradigm, but perhaps future frameworks can facilitate such things.