[英]variable scope inside try catch block
嘗試在Finally
代碼塊中關閉文件時出現錯誤:
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
上方聲明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
是關閉的,但是可以使用更加符合ADOMATIC的C#方式。 StreamReader
的IDisposable.Dispose
實現將關閉流
嘗試之前聲明變量:
StreamReader myReader = null;
等等。然后在try塊中進行設置。
您需要在try
/ catch
/ finally
塊之外聲明StreamReader
實例。
static void Main(string[] args)
{
using (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
{
myReader.Close();
}
}
Console.ReadLine();
}
只需在try塊之外定義myReader
,然后在調用close的finally塊中檢查null
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
{
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.