IOException Class

System.IO
Represents errors that occur during a base-class I/O operation.

Inheritance

System.Object
System.Exception
System.SystemException
System.IO.IOException

Remarks

The IOException class is the base class for exceptions thrown by the .NET Framework class library for I/O errors. These errors include problems with the file system, network access, or other underlying I/O mechanisms.

When an exception is thrown, the IOException object contains the ID of the related error and a message that describes the error. This message typically comes from the operating system.

If an I/O operation fails, an IOException is thrown. For example, attempting to read from a file that does not exist will throw a FileNotFoundException, which derives from IOException.

Constructors

Name Description
IOException() Initializes a new instance of the IOException class.
IOException(String message) Initializes a new instance of the IOException class with a specified error message.
IOException(String message, Exception innerException) Initializes a new instance of the IOException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Properties

Name Description
Data Gets an informational collection of the current exception.
HelpLink Gets or sets a link to the help file that is associated with this exception.
HResult Gets or sets HRESULT, which is a numerical value that is assigned to a specific exception.
InnerException Gets a reference to the inner exception that is the cause of the current exception.
Message Gets an error message that explains the reason for the exception.
Source Gets or sets the name of the application or the object that causes the error.
StackTrace Gets a string representation of the frames in the call stack that leads up to the execution of this exception.
TargetSite Gets the method that throws the current exception.

Methods

Name Description
GetObjectData(SerializationInfo info, StreamingContext context) When overridden in a derived class, sets the SerializationInfo with information about the exception.
ToString() Returns a string representation of the current exception.

Example Usage


using System;
using System.IO;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            // Attempt to open a non-existent file
            using (StreamReader reader = new StreamReader("non_existent_file.txt"))
            {
                string line = reader.ReadToEnd();
                Console.WriteLine(line);
            }
        }
        catch (FileNotFoundException fnfEx)
        {
            Console.WriteLine($"Error: {fnfEx.Message}");
            Console.WriteLine($"File name: {fnfEx.FileName}");
        }
        catch (IOException ioEx)
        {
            // This catch block will handle other IO related exceptions
            Console.WriteLine($"An IO error occurred: {ioEx.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An unexpected error occurred: {ex.Message}");
        }
    }
}
                

See Also