【问题标题】:C# Return Statement In Try/Catch Not Stopping ProgramTry/Catch 中的 C# Return 语句不停止程序
【发布时间】:2018-02-07 02:33:06
【问题描述】:

我创建了一个带有 Web 浏览器对象的 Windows 窗体项目。我创建了一种从 CSV 文件读取到列表中的方法。一旦填充了列表并加载了表单,列表中的第一项将出现在网站的文本框中。

我正在使用 try/catch 块来执行错误处理。我注意到如果文件已经打开,它会显示消息框,但是一旦我关闭消息框,代码就会继续运行。

当浏览器导航到网页时,它会抛出一个参数超出范围异常。

代码继续运行是否正确。我应该在将网络浏览器导航到网站之前添加额外的错误处理吗?

private void LoadAccounts()
{
        if (!File.Exists(path))
        {
            MessageBox.Show("File Doesn't Exist");
        }

            try
            {
                using (StreamReader reader = new StreamReader(path))

                    while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string[] accountinfo = line.Split(',');
                    accounts.Add(new WaterAccount(accountinfo[0], accountinfo[1], accountinfo[2],accountinfo[3],string.Empty, string.Empty));
                }

            }

            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

 }

这是调用 LoadAccounts 的代码块:

public FormWRB()
{
        InitializeComponent();
        LoadAccounts();
        webBrowserWRB.Navigate("https://secure.phila.gov/WRB/WaterBill/Account/GetAccount.aspx");
        buttonExport.Enabled = false;
}

【问题讨论】:

  • 您是否要停止整个程序?也许试试 Application.Exit();虽然,通过整个应用程序的正常逻辑停止程序可能更好,但它更易于维护。是的,程序继续运行是正确的,只要在您调用 LoadAccounts() 之后还有更多代码可以运行
  • 你能把调用LoadAccounts的代码贴出来吗?这将使答案更深刻,与您手头的问题更密切相关
  • 你可以在你的catch语句return false中将LoadAccounts()改为bool而不是void,并在调用LoadAccounts()的代码块中相应地处理结果
  • 如果我想确保在单击消息框之前关闭该文件,最好的做法是什么?向用户添加消息?
  • 谢谢。我决定在调用 LoadAccounts() 的代码块中处理结果

标签: c# return try-catch


【解决方案1】:

try catch 语句不会阻止程序运行,它只是让您有机会处理异常。最常见的记录异常。 本质上,您的代码正在尝试运行一段代码,如果它捕获到 IOException,则在消息框中显示错误消息,并且 return 只是终止该方法的执行。

这里有一个适合您的解决方案。

    private void LoadAccounts()
    {
        if (!File.Exists(path))
        {
            throw new FileNotFoundException($"{path} does not exist");
        }

        using (StreamReader reader = new StreamReader(path))
        {
            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] accountinfo = line.Split(',');
                accounts.Add(new WaterAccount(accountinfo[0], accountinfo[1], accountinfo[2], accountinfo[3], string.Empty, string.Empty));
            }
        }
    }

然后在处理LoadAccounts()的代码块中

    try
    {
        LoadAccounts();
        webBrowserWRB.Navigate("https://secure.phila.gov/WRB/WaterBill/Account/GetAccount.aspx");
        buttonExport.Enabled = false;
    }
    catch (FileNotFoundException ex)
    {
        // Do stuff when file not found here...
    }
    catch (IOException ex)
    {
       // Handle other exceptions here
    }

参考资料: return - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return try catch - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch

【讨论】:

    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2016-03-07
    • 2012-04-18
    • 2013-03-17
    • 1970-01-01
    相关资源
    最近更新 更多