views:

20

answers:

0

Assume I have method that reads customers from a csv file.

When reading the file an exception occurs (e.g. IOException or FormatException). In most cases the method can't do anything about the errors but it can add extra information like file name, line number etc.

The method's class got an IProgressAndErrorReporter dependency that is used to log/display the error.

How do I report back to the caller that an error occured but that it is already taken care of?

Currently I do it like this:

class CustomerReader
{
    private readonly IProgressAndErrorReporter mProgressAndErrorReporter;

    public bool TryReadCustomers() {
        try {
            ...
            return true;
        }
        catch (IOException err) {
            mProgressAndErrorReporter.ReportError(err, "Can't read file {0} bla bla");
            return false;
         }
    }
 }

(I do only catch "expected" exceptions, i.e. not InvalidOperation etc)
I works fine but in the type of data processing applications I usually work with the level above looks like:

public bool TryProcessData() {
    if (!customerReader.TryReadCustomers())
        return false;

    var orders = webOrderReader.ReadNewOrders());
    if (orders == null)
        return false;

    if (!orderCreator.TryCreateNewOrder(order))
    ...

}

To me it feels like going back to the time before exceptions.

Should I throw some kind of "AlreadyHandledException" instead with an empty catch clause at the top level?