我在“异常”部分遇到错误,我不知道为什么

use*_*704 1 c# exception try-catch

当我运行这段代码时,我在 catch(异常 e)部分出现错误,我不知道为什么,编译器说“不能在此范围内声明名为 'e' 的局部变量,因为它会给 ' 赋予不同的含义e',它已经在“父级或当前”范围中使用来表示其他内容”

        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();
        }
Run Code Online (Sandbox Code Playgroud)

希望您的回复!谢谢!

lc.*_*lc. 5

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

您很可能处于带有指定EventArgs参数的事件处理程序中e,并且您应该将其中一个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??
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  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??
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 匿名函数(lambda)

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

请注意,以下内容不会导致相同的错误,因为您可以使用关键字消除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
        }
    }

}
Run Code Online (Sandbox Code Playgroud)