C# try-catch块内的变量范围
尝试关闭Finally代码块中的文件时出错:C# try-catch块内的变量范围,c#,exception-handling,scope,C#,Exception Handling,Scope,尝试关闭Finally代码块中的文件时出错: static void Main(string[] args) { try { StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt"); string line = ""; while (line != null) { line = myReader.ReadLine();
static void Main(string[] args)
{
try
{
StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
myReader.Close();
}
Console.ReadLine();
}
您需要在try/catch/finally块之外声明StreamReader实例
您需要在try上面声明StreamReader 尽管如此,我还是建议在本例中使用using语句而不是try/finally,因为它是专门为资源清理而设计的
using (StreamReader myReader = new StreamReader("c:\\j\\MyFile1.txt"))
{
try
{
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
}
Console.ReadLine();
这将保证StreamReader是关闭的,但要以更为idomatic的方式进行。StreamReader的IDisposable.Dispose实现将关闭流只需在try块外定义myReader,并在finally块中检查null,然后调用close
StreamReader myReader = null;
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
//.....
在最后一个街区
finally
{
// Performs the operations that should be accomplished for eg closing the connections, file, database
if(myReader != null)
myReader.Close();
}
在尝试之前声明变量:
StreamReader myReader = null;
等等。然后在try块中设置它们。这样做
StreamReader myReader = null;
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
// Performs the operations that should be accomplished for eg closing the connections, file, database
if(myReader !=null)
myReader.Close();
}
Console.ReadLine();
}
}
您是要告诉我们错误还是我们猜猜看?错误:名称“myReader”在当前上下文中不存在,因为它的作用域在块中,所以可以保留在try块中。请通过代码示例说明这一点,因为我尝试在try块外声明它,但它不工作。错误:名称“myReader”在当前上下文谢谢大家在那里帮助我!我在你们的帮助下修好了!上帝保佑,谢谢你们每个人帮助我!我在你们的帮助下修好了!上帝保佑!
StreamReader myReader = null;
try
{
myReader = new StreamReader("c:\\j\\MyFile1.txt");
string line = "";
while (line != null)
{
line = myReader.ReadLine();
Console.WriteLine(line);
}
//myReader.Close();
}
catch (FileNotFoundException e)
{
Console.WriteLine("sorry file not found! {0}", e.Message);
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine("You have given the wrong path for the file! {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Sorry you typed in a wrong file name! {0}", e.Message);
}
finally
{
// Performs the operations that should be accomplished for eg closing the connections, file, database
if(myReader !=null)
myReader.Close();
}
Console.ReadLine();
}
}