FileNotFoundException

Namespace: System.IO Inheritance: ExceptionIOExceptionFileNotFoundException

Description

The exception that is thrown when an attempt to access a file that does not exist on disk fails. This exception is typically thrown by members of the System.IO namespace, such as the File.Open method.

Syntax

public sealed FileNotFoundException : IOException

Constructors

Constructor Description
FileNotFoundException() Initializes a new instance of the FileNotFoundException class.
FileNotFoundException(string message) Initializes a new instance of the FileNotFoundException class with a specified error message.
FileNotFoundException(string message, Exception innerException) Initializes a new instance of the FileNotFoundException class with a specified error message and a reference to the inner exception that is the cause of this exception.
FileNotFoundException(string message, string fileName) Initializes a new instance of the FileNotFoundException class with a specified error message and the name of the file that is not found.
FileNotFoundException(string message, string fileName, Exception innerException) Initializes a new instance of the FileNotFoundException class with a specified error message, the name of the file that is not found, and a reference to the inner exception that is the cause of this exception.

Properties

Property Description
Data Gets a collection of key/value pairs that provide additional user-defined information about the exception.
FileName Gets the name of the file that could not be found.
FileName Gets the name of the file that could not be found.
InnerException Gets a reference to the inner exception that is the cause of the current exception.
Message Gets a message that describes the current exception.
Source Gets or sets a value indicating the application or object that causes the error.
StackTrace Gets a string representation of the immediate frames of the call stack.
TargetSite Gets a reference to the method that throws the current exception.

Methods

Method Description
GetBaseException() When overridden in a derived class, returns the Exception that is the root cause of the current exception.
GetObjectData(SerializationInfo info, StreamingContext context) Sets the SerializationInfo with information about the exception.

Example Usage

try
{
    // Attempt to access a file that may not exist
    FileStream fs = new FileStream("non_existent_file.txt", FileMode.Open);
    // ... process the file stream ...
    fs.Dispose();
}
catch (FileNotFoundException ex)
{
    // Handle the exception
    Console.WriteLine($"Error: The file '{ex.FileName}' was not found.");
    Console.WriteLine(ex.Message);
    // You can also log the exception details for debugging
    // Console.WriteLine(ex.StackTrace);
}
catch (IOException ex)
{
    // Handle other potential IO errors
    Console.WriteLine($"An IO error occurred: {ex.Message}");
}

Remarks

The FileNotFoundException is thrown when an operation cannot find the specified file. This can occur for various reasons, including:

When handling this exception, it is recommended to check the value of the FileName property to identify the specific file that caused the error. You may then prompt the user to provide a correct path, recreate the file, or inform them of the issue.

Exceptions

The FileNotFoundException is thrown by various methods in the System.IO namespace. Some common scenarios include: