为什么我的“ using”语句之外的代码无法访问?

时间:2019-02-19 05:16:12

标签: c#

如果using语句中引发了异常,则应立即调用finally块并处理该对象。因此,try块的其余部分不应该执行,对吧?

 public static async Task<bool> AddCharName(string id, string name)
        {
            using (var dbConn = new DbConn())
            {
                await dbConn.Connection.OpenAsync();

                command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

                using (MySqlCommand update = dbConn.Connection.CreateCommand())
                {
                    update.CommandText = command;
                    update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
                    update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
                    await update.ExecuteNonQueryAsync();
                    return true;
                }
            }
            return false;
        }

在我的示例中,return false发出“检测到无法访问的代码”警告。我是否正确认为这是一个错误?就像我在思考,如果在using (MySqlCommand upd...中抛出错误,那么它应该返回false吗?

5 个答案:

答案 0 :(得分:3)

真的很简单...

总是

返回
return true;

它永远没有机会去

return false;

这是编译器最了解的情况

更新

  

确定吗?因此,如果using语句中发生异常,则在   返回true,它还会返回true吗?

尽管using最终实现了 try ,但它仍会引发原始异常,因此仍然不会到达return false;(无论发生什么情况)

答案 1 :(得分:3)

实际上,using是语法语法,可以最终尝试使用,但是因为没有捕获块。换句话说,您拥有:

MySqlCommand update = dbConn.Connection.CreateCommand();
try
{
    update.CommandText = command;
    update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
    update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
    await update.ExecuteNonQueryAsync();
    return true;
}
finally
{
    update.Dispose();
}

请注意,没有catch,因此您的代码也可以:

  1. 到达return true;并返回那里,或者
  2. 引发异常。 finally块将执行处理对象的操作,然后异常立即冒泡到调用方(如果没有处理程序,则崩溃)。

在任何情况下,执行都不会超出您的return true;语句。当然,外部using块也是如此。


现在,您最可能想做的是发生catch个异常。快速而肮脏的方法是将所有内容包装在try-catch中,尽管您可能希望根据应用程序更详细地了解在何处捕获什么以及如何捕获:

try
{
    using (var dbConn = new DbConn())
    {
        await dbConn.Connection.OpenAsync();

        command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

        using (MySqlCommand update = dbConn.Connection.CreateCommand())
        {
            update.CommandText = command;
            update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
            update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
            await update.ExecuteNonQueryAsync();
            return true;
        }
    }
}
catch (MySqlException)
{
    return false;
}

答案 2 :(得分:3)

无法访问代码,因为上面已经有return true

使用try catch

try
            {
                using (var dbConn = new DbConn())
                {
                    await dbConn.Connection.OpenAsync();

                    command = "UPDATE user SET name = @name WHERE name IS NULL AND id = @id";

                    using (MySqlCommand update = dbConn.Connection.CreateCommand())
                    {
                        update.CommandText = command;
                        update.Parameters.Add("@name", MySqlDbType.VarChar).Value = name;
                        update.Parameters.Add("@id", MySqlDbType.VarChar).Value = id;
                        await update.ExecuteNonQueryAsync();
                        return true;
                    }
                }
            }
            catch (Exception er)
            {
                return false;
            }

答案 3 :(得分:1)

它将进入finally块,但是在final完成后仍会引发异常,因此在这种情况下它将无法返回值。

答案 4 :(得分:0)

它的显示是因为如果使用block时发生异常,它将首先处理自身,然后捕获catch并导致您的return false;无法访问。我建议将值保留在变量中。

相关问题