Represents errors that occur when the data in a stream is invalid. This exception is typically thrown when the format or content of data being read from a stream does not conform to expected standards.
Namespace: System.IO
Assembly: mscorlib.dll (in .NET Framework 1.0)
Inheritance: Object > Exception > SystemException > IOException > InvalidDataException
Initializes a new instance of the InvalidDataException
class with a system-supplied message that describes the error.
Initializes a new instance of the InvalidDataException
class with a specified error message.
Parameters:
message
: The error message that explains the reason for the exception.Initializes a new instance of the InvalidDataException
class with a specified error message and a reference to the inner exception that is the cause of this exception.
Parameters:
message
: The error message that explains the reason for the exception.innerException
: The exception that is the cause of the current exception. If innerException
is not null
, the current exception is raised in a catch
block that handles innerException
.Gets a collection of key/value pairs that provide additional user-defined data about the exception. (Inherited from Exception)
Gets or sets a link to the help file that is associated with this exception. (Inherited from Exception)
Gets or sets the HRESULT value that is assigned to the specific exception. (Inherited from Exception)
Gets the exception that is the cause of the current exception. (Inherited from Exception)
Gets a message that describes the current exception. (Inherited from Exception)
Gets or sets the name of the application or the object that causes the error. (Inherited from Exception)
Gets a string representation of the immediate frames of the call stack. (Inherited from Exception)
Gets the method that throws the current exception. (Inherited from Exception)
The following code example demonstrates how to catch an InvalidDataException
when reading a file that is expected to be in a specific format.
using System;
using System.IO;
using System.Text;
public class Example
{
public static void Main()
{
try
{
// Assume this simulates reading from a corrupted file
ProcessData("corrupted_data.bin");
}
catch (InvalidDataException ex)
{
Console.WriteLine("An error occurred while processing data:");
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine($"Stack Trace: {ex.StackTrace}");
}
catch (IOException ex)
{
Console.WriteLine($"An IO error occurred: {ex.Message}");
}
}
public static void ProcessData(string filePath)
{
using (FileStream fs = new FileStream(filePath, FileMode.Open))
using (BinaryReader reader = new BinaryReader(fs))
{
// Simulate reading data that is expected to be a certain format
// but turns out to be corrupted.
// For example, if we expect an integer but read bytes that don't form a valid int.
if (fs.Length < 4) // Example validation
{
throw new InvalidDataException("File is too short to contain valid data.");
}
byte[] buffer = new byte[4];
int bytesRead = fs.Read(buffer, 0, buffer.Length);
if (bytesRead < 4)
{
throw new InvalidDataException("Could not read enough bytes for data.");
}
// Attempt to interpret the data (this could lead to InvalidDataException if malformed)
// For demonstration purposes, let's assume some arbitrary interpretation logic that might fail.
// In a real scenario, this would involve parsing specific structures.
// Example: trying to interpret as a string
try {
string data = Encoding.UTF8.GetString(buffer);
if (data.Contains("INVALID")) // Arbitrary check
{
throw new InvalidDataException("The data content is invalid.");
}
Console.WriteLine($"Read data: {data}");
} catch (Exception e) {
throw new InvalidDataException("Failed to parse data format.", e);
}
}
}
}
The InvalidDataException
exception is thrown when data read from a stream is not in the expected format. For instance, when reading a file that should contain specific structured data, and the content does not match the expected schema, this exception might be thrown.
This exception derives from IOException
, indicating that the error is related to input/output operations.