Exception handling - exception in catch clause?

I am loading data from text files into db tables. Data in files is sometimes corrupted at the field level (files are comma-delimited .csv files)

I read every row in an object that represents a row of data with properties that are the correct data type.

If reading from an object is not performed due to ingenious data, I want to read a string into a similar object as the first, only this one has all the data types set to a string, so reading into it should not be fal.

The idea is that I can create a set of valid record objects that I will load into the corresponding db table, and a set of exceptions that I will load into the exception table. Then they can be considered later.

So - the question is: I'm going to iterate over the lines of a text file and load them into an object and add the object to the collection. There will be a try / catch loop, and if loading the object fails, then in the catch section I will load the exception object and add it to the exception collection.

However, what happens if the error of the exception object fails (for some reason). Do I put try / catch around this and log exceptions - e.g. try / catch in try / catch?

Is there a better way to do this?

+4
source share
3 answers
  • The code inside the block catchis no different from other code.

, try catch, .

2. , try - if. if-statements .

+5

. Try-Catch catch. .

Imapler, , . . , , , Try-Catch Catch.

var exceptionList = new List<ExceptionLines>();

try
{
    // read lines, parse...
}
catch(Exception ex)
{
    // handle the lines with the exception. Add the exception and the necessary arguments to the collection
    exceptionList.Add( new ExceptionLines(....));
}

// do stuff

// handle the exceptions.
foreach(var exception in exceptionList)
{
    try
    {
        // process the exception line.
    }
    catch(Exception ex)
    {
          // log error and handle exception
    }
}

. , .

// somewhere in your code...
WrapException( () =>
{
    // read and parse lines...
}, (ex) =>
{
    WrapException(ex, ParseToExceptionFunction, HandleExceptionParseFunction, false);

}, false);

void WrapException(Action func, Action<Exception> handleCatch, bool rethrow)
{
    try
    {
        func();
    }
    catch(Exception ex)
    {
        handleCatch(ex);

        if (rethrow)
            throw;
    }
}

static void WrapException<T>(T arg, Action<T> func, Action<Exception> handleCatch, bool rethrow)
{
    try
    {
        func(arg);
    }
    catch (Exception ex)
    {
        handleCatch(ex);

        if (rethrow)
                throw;
    }
}

void ParseToExceptionFunction(ArgType arg)
{
    // try to parse to excetion
}

void HandleExceptionParseFunction(Exception ex)
{
    // handle the exception for parsing the line with the exception
}

ParseToExceptionFunction HandleExceptionParseFunction lambdas...

+1

.

Used if otherwise, in order to catch ingenious data, I created a list of good data and a list of exceptions (raised by if and else), and then processed the lists in try / catch to catch any other exceptions that could be arile (ref integrity problems, etc. d.)

Thanks for the advice.

0
source

Source: https://habr.com/ru/post/1546254/


All Articles