FileNotFoundException
Namespace:
System.IO
Inheritance: Exception
→ IOException
→ FileNotFoundException
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 |
---|---|
|
Gets a collection of key/value pairs that provide additional user-defined information about the exception. |
|
Gets the name of the file that could not be found. |
|
Gets the name of the file that could not be found. |
|
Gets a reference to the inner exception that is the cause of the current exception. |
|
Gets a message that describes the current exception. |
|
Gets or sets a value indicating the application or object that causes the error. |
|
Gets a string representation of the immediate frames of the call stack. |
|
Gets a reference to the method that throws the current exception. |
Methods
Method | Description |
---|---|
|
When overridden in a derived class, returns the Exception that is the root cause of the current exception. |
|
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:
- The file path is incorrect or misspelled.
- The file has been moved or deleted.
- The application does not have sufficient permissions to access the file.
- The file is being used by another process and is locked.
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:
- Using methods like
File.Open
,File.OpenRead
, orFile.OpenWrite
with a file that does not exist. - Attempting to read from or write to a file that has been deleted or moved during the operation.