Following on from [exception.message], if a method needs to be able to return multiple data items, the temptation is to write a new exception that has all the data in it, and throw the exception back to to the caller.

public void determineNextFileStatus() throws StatusException
{
    :
    StatusException e = new StatusException();
    e.setPath(nextFile.getAbsolutePath());
    e.setLength(nextFile.length());
    e.setValid(!hasErrors);
    throw e;
}

Don't do it.  Exceptions are for exceptional circumstances only, and are only supposed to contain error data.  Note that in the above example, the developer used the vague word “determine” because their method threw an exception instead of returning anything.

Far better to split the method call in two or return a new object with many properties:

public FileStatus getNextFileStatus()
{
    :
    FileStatus status = new FileStatus();
    status.setPath(nextFile.getAbsolutePath());
    status.setLength(nextFile.length());
    status.setValid(!hasErrors);
    return status;
}
blog comments powered by Disqus