R
RayLopez99
Examine the below. It works, but my question is, since 'using' is
equivalent to 'try/catch', why do we need the outer try/catch block to
catch the situation where "MyFile.txt" does not exist?
RL
try
{
using (FileStream fs = File.OpenRead
("MyFile.txt")) //ASSUME "MyFile.txt" does not exist
{
using (TextReader reader = new StreamReader(fs))
{
StringBuilder myStringBuilderString = new
StringBuilder();
myStringBuilderString = new StringBuilder
(reader.ReadToEnd()); //read in entire string to end
}
}
}
catch (IOException Ex)
{
MessageBox.Show("Error: could not read file: " +
Ex.Message);
}
equivalent to 'try/catch', why do we need the outer try/catch block to
catch the situation where "MyFile.txt" does not exist?
RL
try
{
using (FileStream fs = File.OpenRead
("MyFile.txt")) //ASSUME "MyFile.txt" does not exist
{
using (TextReader reader = new StreamReader(fs))
{
StringBuilder myStringBuilderString = new
StringBuilder();
myStringBuilderString = new StringBuilder
(reader.ReadToEnd()); //read in entire string to end
}
}
}
catch (IOException Ex)
{
MessageBox.Show("Error: could not read file: " +
Ex.Message);
}