什么是预期捕获或最终的错误 [英] what is the error in expected catch or finally

查看:97
本文介绍了什么是预期捕获或最终的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

This is my code:

private void txtCountryCode_Enter(object sender, EventArgs e)
       {
           try
           {
               if (txtVehClass.Text.Trim() == string.Empty)

                   return;

                   string strQry = string.Empty;
                   strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";

                   DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);

                   if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)

                       txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
                       txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
                       txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
                   }
               }

           }
   }



错误预计会捕获或最终...如何解决这个错误


the error is expected catch or finally...how to solve this error

推荐答案

应该是这样的:



Should be like this:

private void txtCountryCode_Enter(object sender, EventArgs e)
{
    try
    {
        //whatever
    }
    catch (Exception exception)
    {
        //if an error occurs with in the try block, it will handled here.
    }
}





我建议你购买一本基本编程书并阅读概念。

阅读更多文档这里是MSDN:



http://msdn.microsoft.com/en-us/library/0yd65esw(v = vs.110).aspx [ ^ ]


private void txtCountryCode_Enter(object sender, EventArgs e)
        {
            try
            {
                if (txtVehClass.Text.Trim() == string.Empty)
                
                    return;
 
                    string strQry = string.Empty;
                    strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";
 
                    DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);
 
                    if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)
                    
                        txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
                        txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
                        txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
                    }
                }
 
            }
            catch(Exception ex)
            {// do exception handling here
            }
            finally
            {// do resource cleanup here, optional in your case
            }
    }


添加一个catch或finally块:

Add a catch or finally block:
private void txtCountryCode_Enter(object sender, EventArgs e)
    {
    try
        {
        if (txtVehClass.Text.Trim() == string.Empty)

            return;

        string strQry = string.Empty;
        strQry = "Select * from VehClass where upper(code)='" + txtVehClass.Text.Trim().ToUpper() + "' and isnull(intstatus,0)=0";

        //DataSet dsGrid = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings["TrafficDB"], CommandType.Text, strQry);

        if (dsGrid != null && dsGrid.Tables.Count > 0 && dsGrid.Tables[0].Rows.Count > 0)
            {
            txtVehClass.Tag = dsGrid.Tables[0].Rows[0]["Intcode"].ToString();
            txtVehClass.Text = dsGrid.Tables[0].Rows[0]["code"].ToString();
            txtVehClassName.Text = dsGrid.Tables[0].Rows[0]["NameArb"].ToString();
            }
        }
    catch (Exception ex)
        {
        // Report, log or otherwise handle the exception
        Console.WriteLine(ex.Message);
        }

    }

请注意,在 if 语句后,您还缺少一个开放的大括号 - 没有它,缩进不会使它执行所有三个语句! (我在这里添加了它,你可以看到我的意思)

Do note that you are also missing an open curly bracket after the if statement - indentation will not make it execute all three statements without it! (I added it here so you can see what I mean)


这篇关于什么是预期捕获或最终的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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