相关疑难解决方法(0)

无法访问的代码,但是有异常可以访问

此代码是读取和写入ODBC连接的数据库的应用程序的一部分。它在数据库中创建一条记录,然后检查是否已成功创建记录,然后返回true

我对控制流的理解如下:

command.ExecuteNonQuery()有记录表明,Invalid?Operation?Exception当“方法调用对该对象的当前状态无效”时,将引发。因此,如果发生这种情况,该try块的执行将停止,该finally块将被执行,然后return false;在底部执行。

但是,我的IDE声称该return false;代码不可访问。而且似乎是事实,我可以删除它,并且可以毫无抱怨地进行编译。但是,对我来说,似乎抛出上述异常的代码路径没有返回值。

private static bool createRecord(String table,
                                 IDictionary<String,String> data,
                                 System.Data.IDbConnection conn,
                                 OdbcTransaction trans) {

    [... some other code ...]

    int returnValue = 0;
    try {
        command.CommandText = sb.ToString();
        returnValue = command.ExecuteNonQuery();

        return returnValue == 1;
    } finally {
        command.Dispose();
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我在这里理解的错误是什么?

c# exception unreachable-code

108
推荐指数
7
解决办法
1万
查看次数

标签 统计

c# ×1

exception ×1

unreachable-code ×1