简体   繁体   English

Try/Catch 不停止程序中的 C# Return 语句

[英]C# Return Statement In Try/Catch Not Stopping Program

I created a Windows form project with a web browser object.我创建了一个带有 Web 浏览器对象的 Windows 窗体项目。 I created a method to reading from a CSV file into a list.我创建了一种从 CSV 文件读取到列表的方法。 And once the list is populated and the form loads, the first item in the list will appear in the website's text box.填充列表并加载表单后,列表中的第一项将出现在网站的文本框中。

I'm using a try/catch block to perform error handling.我正在使用 try/catch 块来执行错误处理。 I noticed that if the file is already open it is showing the message box, but the code continues to run once I close the message box.我注意到如果文件已经打开,它会显示消息框,但是一旦我关闭消息框,代码就会继续运行。

It is then throwing an Argument Out of Range Exception when the browser navigates to the web page.当浏览器导航到网页时,它会抛出一个参数超出范围异常。

Is it correct that the code continues to run.代码继续运行是否正确。 Should I just add additional error handling before navigating the web browser to the website?我应该在将 Web 浏览器导航到网站之前添加额外的错误处理吗?

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;
            }

 }

This is the code block calling LoadAccounts:这是调用 LoadAccounts 的代码块:

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

A try catch statement does not stop the program from running, it just affords you the chance to handle an exception. try catch 语句不会阻止程序运行,它只是为您提供处理异常的机会。 Most commonly logging the exception.最常见的是记录异常。 Essentially what your code is doing is trying to run a block of code, if it catches an IOException then display the error message in a message box and the return just terminates the execution of the method.本质上,您的代码正在尝试运行代码块,如果它捕获IOException则在消息框中显示错误消息,并且返回值只是终止方法的执行。

Here's a solution that could work for you.这是一个可以为您工作的解决方案。

    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));
            }
        }
    }

Then in the code block that handles LoadAccounts()然后在处理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
    }

References: 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参考: return - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/return try catch - https://docs.microsoft.com/en-us/dotnet/csharp/语言参考/关键字/try-catch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM