如何使用异常使用流构建器 [英] how to use stream builders using exceptions

查看:83
本文介绍了如何使用异常使用流构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个使用文本框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-catch块没有意义.

也许最好使用这个:

So strange code style.. I think, in your case there is no sence for nested try-catch block.

Maybe it''s better to use this:

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.


多余的try..catch没有任何逻辑,因为唯一的声明是StreamReader类的声明和实例化.从构造函数中引发异常以免发生这种情况是非常糟糕的做法. (除非内存被该新类填满,否则将导致内存不足,但这不是您需要考虑的事情).可以安全地除去外部try.catch.

也许更好的方法是使用使用".这样可以确保在超出范围时正确销毁对象(流将被关闭),这样就不需要final.

There isn''t any logic for the extra surrounding try..catch because the only statement is the declaration and instantiation of a StreamReader class. It would be very bad practice to throw an exception from a constructor so that won''t happen. (unless memory is that filled up that this new class will give an out-of-memory but that is not something you need to consider). The outer try.catch therefor can be removed safely.

Maybe a better way is to use "using". This will ensure proper destruction of the object when out of scope (stream will be closed) and that way the finally isn''t needed.

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!


这篇关于如何使用异常使用流构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆