FileHelpers - ErrorMode.ThrowException

  • Contact / Feedback
  • Introduction
  • Download
  • Quick Start
  • Must Read
  • Examples
    • QuickStart
      • Read Delimited File
      • Write Delimited File
      • Read Fixed File
      • Write Fixed File
      • Read or Write Record by Record
      • Autoproperties
    • Missing Values
      • Handle Missing Values with Nullable
      • Handle Missing Values With FieldNullValue
    • Attributes
      • FieldTrim
      • FieldOrder
      • FixedLengthRecord FixedMode.AllowLessChars
    • Converters
      • Enum Converter
      • Custom Converter
    • Events And Notification
      • INotifyRead Interface
      • INotifyWrite Interface
      • Before/After Read Event Handling
      • Before/After Write Event Handling
    • ErrorHandling
      • ErrorMode.ThrowException
      • ErrorMode.IgnoreAndContinue
      • ErrorMode SaveAndContinue
    • Advanced
      • Dynamic Engine Options
      • Multiple Delimiters
      • Multi Record Processing
      • Smart Format Detector
    • Sorting
      • Sort Big File with Record Class
      • Sort Big File without Record Class 1
      • Sort Big File without Record Class 2
    • MasterDetail
      • Master Detail Custom Selector
      • Master Detail Common Selector
  • Credits
  • Invite us a beer
  • Contribute
  • Source Code
  • Api Docs (Sandcastle)
  1. Examples
  2. ErrorHandling
  3. ErrorMode.ThrowException
Previous: Before/After Write Event Handling
Next: ErrorMode.IgnoreAndContinue
Click button to edit
ErrorHandling: ErrorMode.ThrowException
Default Behavior. Read the file or reject the whole file

Run an example of running a file with an error through and using a try catch to collect the error.

In the standard mode you can catch the exceptions when something fails.

Input.txt
ALFKI|Alfreds Futterkiste|Maria Anders|SalesRepresentative
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|NotInEnum
FRANR|France restauration|Carine Schmitt|MarketingManager
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner
Customers with Enum.cs
[DelimitedRecord("|")]
public class Customer
{
    public string CustomerID;
    public string CompanyName;
    public string ContactName;
    public CustomerTitle ContactTitle;
}

public enum CustomerTitle
{
    Owner,
    SalesRepresentative,
    MarketingManager
}
Example.cs
try
{
    var engine = new DelimitedFileEngine<Customer>();
    
    //  This fails with not in enumeration error
    var customers = engine.ReadFile("Input.txt");
    
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString()); // with stack trace
}
Console
FileHelpers.ConvertException: Error Converting 'NotInEnum' to type: 'CustomerTitle'. The value NotInEnum is not present in the Enum.
   at FileHelpers.EnumConverter.StringToField(String from) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Converters\EnumConverter.cs:line 25
   at FileHelpers.FieldBase.AssignFromString(ExtractedInfo fieldString, LineInfo line) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Fields\FieldBase.cs:line 711
   at FileHelpers.FieldBase.ExtractFieldValue(LineInfo line) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Fields\FieldBase.cs:line 573
   at FileHelpers.RecordOperations.StringToRecord(Object record, LineInfo line, Object[] values) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Core\RecordOperations.cs:line 87
   at FileHelpers.FileHelperEngine`1.ReadStreamAsList(TextReader reader, Int32 maxRecords, DataTable dt) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Engines\FileHelperEngine.cs:line 282
   at FileHelpers.FileHelperEngine`1.ReadStream(TextReader reader, Int32 maxRecords) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Engines\FileHelperEngine.cs:line 145
   at FileHelpers.FileHelperEngine`1.ReadFile(String fileName, Int32 maxRecords) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Engines\FileHelperEngine.cs:line 102
   at FileHelpers.FileHelperEngine`1.ReadFile(String fileName) in D:\Desarrollo\Devoo\GitHub\FileHelpers\FileHelpers\Engines\FileHelperEngine.cs:line 92
   at ExamplesFx.ErrorModeThrowException.Run()
Copyright © 2020 Devoo - Marcos Meli All rights reserved. Template Design by GeeksLabs