是否可以使用try和catch跳过用户输入? [英] Is it possible to skip a user input using a try and catch?

查看:118
本文介绍了是否可以使用try和catch跳过用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个访问各种sql服务器的代码,将数据提取到dataTable,然后计算这些不同表之间的平均值,然后将其显示在webform上。



长话短说:我希望程序绕过/忽略无效的用户输入,仍然以表格的形式显示平均值。



这是我的意思尝试以下:



I've created a code that accesses various sql servers extracts data to a dataTable and then calculates the average between those various tables to then display it on a webform.

Long story short: I wanted the program to bypass/ignore invalid user inputs and still display the averages in the form a table.

Here is what I've tried below:

getUserInput();
                foreach (string item in itemList)
                {//
                    connectionString = string.Format(theString, item); //my modified connection string to allow user to open sql server connection.
                    using (SqlConnection sqlConnect = new SqlConnection(connectionString))
                    {
                                try
                                {
                                    sqlConnect.Open();
                                }
                                catch
                                {
                                    continue;
                                }
                                    sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
createTable();
}





我的尝试:





What I have tried:

try
                                {
                                    sqlConnect.Open();
                                }
                                catch
                                {
                                    continue;
                                }

推荐答案

如果Connection无法打开,则无法执行任何操作:您无法继续使用连接 - 这就像打开车里的收音机来覆盖柏油碎石路面上的金属声,因为所有四个轮胎都已经爆裂......它不会神奇地消失并在下次尝试时工作。



尝试...抓住是不是可以忽略问题 - 它就在那里你可以优雅地失败并在没有你的申请的情况下报告它们撞坏你的用户。



If the Connection fails to open, there is nothing more you can do: you cannot proceed to use the connection - that's like turning up the radio in your car to cover the sound of metal on tarmac because all four tires have burst ... It's not going to "magically go away" and work the next time you try.

try...catch isn't there to ignore problems - it's there so you can gracefully fail and report them without your application crashing out on your user.

引用:

我是不要试图忽略每个问题的问题,我试图让程序生成平均表,而不管连接失败。



所以,如果我有3个连接;列表中的第一个和第三个连接工作,但第二个连接没有。我不希望foreach循环在第二个连接上结束,我希望它从两个工作连接中获取信息以形成表。





然后try块必须包含所有使用它的代码,而不仅仅是尝试打开连接:



Then the try block has to surround ALL the code that uses it, not just the attempt to open the connection:

foreach (string item in itemList)
    {
    try
        {
        connectionString = string.Format(theString, item); /
        using (SqlConnection sqlConnect = new SqlConnection(connectionString))
            {
            sqlConnect.Open();
            sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
            createTable();
            ...
            }
        }
    catch {} // Ignore connections that don't work: we want the average of the others.
    }


在C#中迭代集合时删除项目CodeAddiction.net [ ^ ]



这个有效! @OriginalGriff感谢您之前尝试过您的建议,但我仍然遇到了同样的错误。我所做的就是下面;它可以工作,但只有当无效不在itemList的末尾时(刚刚结束了一条消息,说明从用户输入中删除无效)。



在我的情况下:

Remove items while iterating a collection in C# | CodeAddiction.net[^]

This works!!! @OriginalGriff Thanks for the help I attempted your suggestion before, however I still had the same error. what I've done is below; it works but only if the invalid isn't at the end of the itemList (just ended a message saying to remove invalid from the user input).

In my case:
foreach (string item in itemList.ToArray())
    {
    try
        {
        connectionString = string.Format(theString, item); /
        using (SqlConnection sqlConnect = new SqlConnection(connectionString))
            {
            sqlConnect.Open();
            sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
            createTable();
            ...
            }
        }
    catch {
itemList.Remove(item);
continue;
} // Ignore connections that don't work: we want the average of the others.
    }


这篇关于是否可以使用try和catch跳过用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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