File Handling and Exception Handling
File Handling and Exception Handling
Code:
using System;
using System.IO;
class FileHandlingAssignment
try
// Perform a specified operation (e.g., counting the number of words in each line)
Console.WriteLine($"Error: {ex.Message}");
LogError(ex);
LogError(ex);
LogError(ex);
return results;
File.AppendAllText(logFile, logMessage);
}
Explanation of the Code:
1. File Handling:
2. Exception Handling:
3. Error Reporting:
o Logs detailed error information (timestamp, exception type, message, stack trace) to a
file (error_log.txt).
Example:
Hello world!
C# is fun to learn.
Line 1: 2 words
Line 2: 5 words
Line 3: 4 words
at FileHandlingAssignment.Main()