0

I'm using the FileHelpers library, version 2.9.9 from NuGet. I'm running into a problem while trying to use the AfterReadRecord event.

When I throw an exception from this event, the exception is not caught by the library. It's my understanding that the library will catch exceptions from AfterReadRecord event and build the ErrorInfo collection from them. Instead I am getting a

"Exception was unhandled by user code" warning in VS2013. "An exception of type 'System.Exception' occurred in FileHelpersTest.exe but was not handled in user code" "Additional information: field1 is invalid".

What am I doing wrong? Thanks in advance!

The code looks like this:

[DelimitedRecord(",")]
public class MyClass
{
    public string Field1;
    public string Field2;
    public string Field3;
}

class Program
{
    static void Main(string[] args)
    {
        FileHelperEngine<MyClass> engine = new FileHelperEngine<MyClass>();
        engine.AfterReadRecord += new FileHelpers.Events.AfterReadHandler<MyClass>(engine_AfterReadRecord);
        engine.ErrorMode = ErrorMode.SaveAndContinue;
        MyClass[] records = engine.ReadString(",2,3");
        ErrorInfo[] errors = engine.ErrorManager.Errors;            
        Console.WriteLine(errors.Count().ToString());
        Console.Read();
    }

    static void engine_AfterReadRecord(EngineBase engine, FileHelpers.Events.AfterReadEventArgs<MyClass> e)
    {
        if (string.IsNullOrEmpty(e.Record.Field1))
            throw new Exception("field1 is invalid");
    } // I get the Exception unhandled warning here.
}

1 Answer 1

1

Your program works fine for me.

Are you sure it's not just Visual Studio informing you that an exception was thrown. If you run the application outside of Visual Studio does it work as expected?

You can change the behaviour of Visual Studio's exception handling from the Debug/Exceptions... menu.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.