C# Program: File opening with exception handling
C# Sharp Exception Handling: Exercise-3 with Solution
Write a C# program that reads a file path from the user and tries to open the file. Handle exceptions if the file does not exist.
Sample Solution:
C# Sharp Code:
using System;
using System.IO;
class Program {
static void Main() {
// Prompt user to input the file path and read the input
Console.Write("Input the file path: ");
string filePath = Console.ReadLine();
try {
// Attempt to open the file using StreamReader within a using block for automatic resource disposal
using(StreamReader reader = new StreamReader(filePath)) {
// File opened successfully
Console.WriteLine("File opened successfully.");
}
} catch (FileNotFoundException) {
// Catch block for handling FileNotFoundException
Console.WriteLine("Error: File not found.");
} catch (IOException ex) {
// Catch block for handling IOException while accessing the file
Console.WriteLine("An error occurred while accessing the file: " + ex.Message);
} catch (Exception ex) {
// Catch block for handling other types of exceptions
Console.WriteLine("An unexpected error occurred: " + ex.Message);
}
}
}
Sample Output:
Input the file path: d:\test.txt File opened successfully.
Input the file path: d:\test11.txt Error: File not found.
Explanation:
In the above exercise,
- In the Main() method, a user can input a file path using Console.Write() and Console.ReadLine(). The input is stored in the filePath variable. .
- The program attempts to open the file using a StreamReader wrapped in a using statement. This ensures that the file is automatically closed and resources are properly disposed of, even if an exception occurs.
- Inside the try block, if the file is opened successfully, it displays the message "File opened successfully." Additional code to read or process the file can be added at this point.
- If a FileNotFoundException occurs, it means the file does not exist or the path is incorrect. This exception is caught, and the program displays the error message "Error: File not found."
- If an IOException occurs, it means there was an error accessing the file. This exception is caught, and the program displays a more general error message with the exception message.
- The final catch block catches any other unexpected exceptions that may occur. It displays a general error message with the exception message.
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Negative number exception handling.
Next: File opening with exception handling.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/csharp-exercises/exception-handling/csharp-exception-handling-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics