在catch区继续

Ann*_*nie 2 c# exception-handling

这是:

// retry
for (int i = 0; i < length; ++i)
{
    try
    {
        sqlCommand.ExecuteNonQuery();
    }
    catch (SqlException e)
    {
        if (e.Number == 64)
        {
            continue;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

相当于:

// retry
for (int i = 0; i < length; ++i)
{
    try
    {
        sqlCommand.ExecuteNonQuery();
    }
    catch (SqlException e) { }
}
Run Code Online (Sandbox Code Playgroud)

(因为在后一种情况下循环将继续)

有什么区别(如果有的话)?

Xia*_*312 8

continue 让你跳过当前循环中的剩余参数,然后跳转到下一个迭代.

鉴于我们现在的代码,它没有任何区别.由于之后没有更多的代码if (e.Number == 64) { continue; }.