Consider a C# code snippet like this (fileexcpt.py):
static void test1()
{
try
{
string text1 = File.ReadAllText(@"dir1\text1.txt");
string text2 = File.ReadAllText("text2.txt");
}
catch (IOException ex)
{
Console.Out.WriteLine(string.Format("Error reading file: {0}", ex.error_filename));
}
}
I hope ex.error_filename or something alike can tell me what file(filepath) caused the IO exception. Can I?
I see examples from https://docs.microsoft.com/en-us/dotnet/standard/io/handling-io-errors, but no such information found.
As for Python, it is natural, like this:
try:
text1 = open(r"dir1\text1.txt", "r").read();
text2 = open("text2.txt", "r").read();
except IOError as ex:
print("Error reading file: {0}".format(ex.filename));