Unreachable code, but reachable with an exception

2020-02-20 05:18发布

This code is part of an application that reads from and writes to an ODBC connected database. It creates a record in the database and then checks if a record has been successfully created, then returning true.

My understanding of control flow is as follows:

command.ExecuteNonQuery() is documented to throw an Invalid​Operation​Exception when "a method call is invalid for the object's current state". Therefore, if that would happen, execution of the try block would stop, the finally block would be executed, then would execute the return false; at the bottom.

However, my IDE claims that the return false; is unreachable code. And it seems to be true, I can remove it and it compiles without any complaints. However, for me it looks as if there would be no return value for the code path where the mentioned exception is thrown.

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;
}

What is my error of understanding here?

9条回答
贪生不怕死
2楼-- · 2020-02-20 06:11

You have two return paths in your code, the second of which is unreachable because of the first. The last statement in your try block return returnValue == 1; provides your normal return, so you can never reach the return false; at the end of the method block.

FWIW, order of exection related to the finally block is: the expression supplying the return value in the try block will be evaluated first, then the finally block will be executed, and then the calculated expression value will be returned (inside the try block).

Regarding flow on exception... without a catch, the finally will be executed upon exception before the exception is then rethrown out of the method; there is no "return" path.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-20 06:13

the finally block would be executed, then would execute the return false; at the bottom.

Wrong. finally doesn't swallow the exception. It honors it and the exception will be thrown as normal. It will only execute the code in the finally before the block ends (with or without an exception).

If you want the exception to be swallowed, you should use a catch block with no throw in it.

查看更多
▲ chillily
4楼-- · 2020-02-20 06:22

When the exception is thrown, the stack will unwind (execution will move out of the function) without returning a value, and any catch block in the stack frames above the function will catch the exception instead.

Hence, return false will never execute.

Try manually throwing an exception to understand the control flow:

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

    // Try this.
    throw new Exception("See where this goes.");

    return returnValue == 1;
} finally {
    command.Dispose();
}
查看更多
登录 后发表回答