【问题标题】:I got an error on 'Exception' part and i don't know why我在“异常”部分出现错误,我不知道为什么
【发布时间】:2013-08-01 01:56:29
【问题描述】:

当我运行此代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说 “不能在此范围内声明名为 'e' 的局部变量,因为它会给 'e' 赋予不同的含义,它已经在 'parent or current' 范围内用于表示其他东西"

        try
        {

            //Form Query which will insert Company and will output generated id 
            myCommand.CommandText = "Insert into Comp(company_name) Output Inserted.ID VALUES (@company_name)";
            myCommand.Parameters.AddWithValue("@company_name", txtCompName);
            int companyId = Convert.ToInt32(myCommand.ExecuteScalar());

            //For the next scenario, in case you need to execute another command do it before committing the transaction

            myTrans.Commit();

            //Output Message in message box
            MessageBox.Show("Added", "Company Added with id" + companyId, MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        catch (Exception e)
        {
            try
            {
                myTrans.Rollback();
            }
            catch (SqlException ex)
            {
                if (myTrans.Connection != null)
                {
                    MessageBox.Show("An exception of type " + ex.GetType() +
                                      " was encountered while attempting to roll back the transaction.");
                }
            }

            MessageBox.Show("An exception of type " + e.GetType() +
                              "was encountered while inserting the data.");
            MessageBox.Show("Record was written to database.");

        }
        finally
        {
            myConnection.Close();
        }

希望得到您的回复!谢谢!

【问题讨论】:

  • 请注意,如果您在 MSDN 中搜索错误代码(如您的案例中的 CS0136),您将获得解释常见案例并展示示例的文章 - Compiler Error CS0136

标签: c# exception try-catch


【解决方案1】:

您在本地范围内的其他位置有一个名为 e 的变量,无法消除两者之间的歧义。

您很可能在带有名为 eEventArgs 参数的事件处理程序中,您应该将其中一个 e 标识符重命名为其他名称。

以下示例说明了这个问题:

  1. 参数名称冲突

    void MyEventHandler(object source, EventArgs e)
    //                                          ^^^
    {
        try
        {
            DoSomething();
        }
        catch (Exception e)
        //              ^^^
        {
            OhNo(e);
            // Which "e" is this? Is it the Exception or the EventArgs??
        }
    }
    
  2. 一个冲突的局部变量

    void MyMethod()
    {
        decimal e = 2.71828;
        //     ^^^
    
        try
        {
            DoSomething();
        }
        catch (Exception e)
        //              ^^^
        {
            OhNo(e);
            // Which "e" is this? Is it the Exception or the Decimal??
        }
    }
    
  3. 匿名函数 (lambda)

    void MyMethod()
    {
        decimal e = 2.71828;
        //     ^^^
    
        var sum = Enumerable.Range(1, 10)
                            .Sum(e => e * e); //Which "e" to multiply?
        //                      ^^^
    }
    

注意以下不会导致相同的错误,因为您可以使用 this 关键字消除歧义:

class MyClass
{
    int e;

    void MyMethod()
    {
        try
        {
            DoSomething(e); //Here it is the Int32 field
        }
        catch (Exception e)
        {
            OhNo(e); //Here it is the exception
            DoSomethingElse(this.e); //Here it is the Int32 field
        }
    }

}

【讨论】:

    【解决方案2】:

    这意味着你之前已经声明了一个名为 e 的变量,现在在同一个代码块中,或者它内部的一个块(这个 try/catch 块)你再次声明它。将异常 e 更改为 Exception except,它可能会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多