问题描述
我制作了一个使用文本框txtdescription查看文件文本数据的应用程序
和button display_message并在按钮事件上编写了以下代码
I made an application to view the text data of a file using one textbox txtdescription
and button display_message and wrote following code on button event
try
{
TextReader tr = new StreamReader("asd.txt");
try
{ txtdescription.Text = tr.ReadToEnd(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ tr.Close(); }
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
应用程序正在运行
Application is running
catch (Exception ex)
{ MessageBox.Show(ex.Message);
当我将定义不存在的文件时,就会发生此捕获
但是我没有理解fllowin try catch try
的概念
this catch will occur when i "ll define that file which do not exist
but i am not getting the concept of fllowin try catch try
txtdescription.Text = tr.ReadToEnd(); }
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally
{ tr.Close(); }
什么时候会发生这种捕获????
Thanx ion advance
when will this catch occur???
Thanx ion advance
推荐答案
try
{
TextReader tr = new StreamReader("asd.txt");
txtdescription.Text = tr.ReadToEnd();
}
catch (ConcreteException ex)
{
MessageBox.Show(ex.Message);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
tr.Close();
}
但是,如果您确实想在代码中获取异常,则可以在
But if you really want to get exception in your code, you can write
tr.Close()
before
;
或者,您可以阅读有关 TextReader ReadToEnd方法 [ ^ ]并考虑大小写可以引发此异常.
Or you can read about TextReader ReadToEnd method[^] and think of case, when this exceptions can be raised.
class Test
{
public static void Main()
{
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
}
}
祝你好运!
Good luck!
这篇关于如何使用异常使用流构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!