// LumenWorks.Framework.IO.Csv.MalformedCsvException // Copyright (c) 2005 Sébastien Lorion // // MIT license (http://en.wikipedia.org/wiki/MIT_License) // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies // of the Software, and to permit persons to whom the Software is furnished to do so, // subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. using System; using System.Globalization; using System.Runtime.Serialization; using System.Security.Permissions; using LumenWorks.Framework.IO.Csv.Resources; namespace LumenWorks.Framework.IO.Csv { /// /// Represents the exception that is thrown when a CSV file is malformed. /// [Serializable()] public class MalformedCsvException : Exception { #region Fields /// /// Contains the message that describes the error. /// private string _message; /// /// Contains the raw data when the error occured. /// private string _rawData; /// /// Contains the current field index. /// private int _currentFieldIndex; /// /// Contains the current record index. /// private long _currentRecordIndex; /// /// Contains the current position in the raw data. /// private int _currentPosition; #endregion #region Constructors /// /// Initializes a new instance of the MalformedCsvException class. /// public MalformedCsvException() : this(null, null) { } /// /// Initializes a new instance of the MalformedCsvException class. /// /// The message that describes the error. public MalformedCsvException(string message) : this(message, null) { } /// /// Initializes a new instance of the MalformedCsvException class. /// /// The message that describes the error. /// The exception that is the cause of the current exception. public MalformedCsvException(string message, Exception innerException) : base(String.Empty, innerException) { _message = (message == null ? string.Empty : message); _rawData = string.Empty; _currentPosition = -1; _currentRecordIndex = -1; _currentFieldIndex = -1; } /// /// Initializes a new instance of the MalformedCsvException class. /// /// The raw data when the error occured. /// The current position in the raw data. /// The current record index. /// The current field index. public MalformedCsvException(string rawData, int currentPosition, long currentRecordIndex, int currentFieldIndex) : this(rawData, currentPosition, currentRecordIndex, currentFieldIndex, null) { } /// /// Initializes a new instance of the MalformedCsvException class. /// /// The raw data when the error occured. /// The current position in the raw data. /// The current record index. /// The current field index. /// The exception that is the cause of the current exception. public MalformedCsvException(string rawData, int currentPosition, long currentRecordIndex, int currentFieldIndex, Exception innerException) : base(String.Empty, innerException) { _rawData = (rawData == null ? string.Empty : rawData); _currentPosition = currentPosition; _currentRecordIndex = currentRecordIndex; _currentFieldIndex = currentFieldIndex; _message = String.Format(CultureInfo.InvariantCulture, ExceptionMessage.MalformedCsvException, _currentRecordIndex, _currentFieldIndex, _currentPosition, _rawData); } /// /// Initializes a new instance of the MalformedCsvException class with serialized data. /// /// The that holds the serialized object data about the exception being thrown. /// The that contains contextual information about the source or destination. protected MalformedCsvException(SerializationInfo info, StreamingContext context) : base(info, context) { _message = info.GetString("MyMessage"); _rawData = info.GetString("RawData"); _currentPosition = info.GetInt32("CurrentPosition"); _currentRecordIndex = info.GetInt64("CurrentRecordIndex"); _currentFieldIndex = info.GetInt32("CurrentFieldIndex"); } #endregion #region Properties /// /// Gets the raw data when the error occured. /// /// The raw data when the error occured. public string RawData { get { return _rawData; } } /// /// Gets the current position in the raw data. /// /// The current position in the raw data. public int CurrentPosition { get { return _currentPosition; } } /// /// Gets the current record index. /// /// The current record index. public long CurrentRecordIndex { get { return _currentRecordIndex; } } /// /// Gets the current field index. /// /// The current record index. public int CurrentFieldIndex { get { return _currentFieldIndex; } } #endregion #region Overrides /// /// Gets a message that describes the current exception. /// /// A message that describes the current exception. public override string Message { get { return _message; } } /// /// When overridden in a derived class, sets the with information about the exception. /// /// The that holds the serialized object data about the exception being thrown. /// The that contains contextual information about the source or destination. public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { base.GetObjectData(info, context); info.AddValue("MyMessage", _message); info.AddValue("RawData", _rawData); info.AddValue("CurrentPosition", _currentPosition); info.AddValue("CurrentRecordIndex", _currentRecordIndex); info.AddValue("CurrentFieldIndex", _currentFieldIndex); } #endregion } }