gpt4 book ai didi

c# - 无法访问的代码,但可以异常访问

转载 作者:IT王子 更新时间:2023-10-29 03:34:52 26 4
gpt4 key购买 nike

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

我对控制流的理解是这样的:

command.ExecuteNonQuery() 被记录为在“方法调用对于对象的当前状态无效”时抛出 Invalid Operation Exception。因此,如果发生这种情况,将停止执行 try block ,执行 finally block ,然后执行 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;
}

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

最佳答案

Compiler Warning (level 2) CS0162

Unreachable code detected

The compiler detected code that will never be executed.

这只是说,编译器通过静态分析充分理解了它无法到达,并从编译的IL中完全省略了它。 (因此你的警告)。

注意:您可以通过使用调试器或使用 IL Explorer 尝试单步执行无法访问的代码来向自己证明这一事实。

finally 可能会在 Exception 上运行,(尽管除此之外)它不会改变事实(在这种情况下)它仍然是一个 未捕获的异常。因此,无论如何,最后一个 return 永远不会被击中。

  • 如果您希望代码继续执行到最后一个返回,您唯一的选择是捕获异常

  • 如果您不这样做,请保持原样并删除 return

示例

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

return returnValue == 1;
}
catch(<some exception>)
{
// do something
}
finally
{
command.Dispose();
}

return false;

引用文档

try-finally (C# Reference)

By using a finally block, you can clean up any resources that areallocated in a try block, and you can run code even if an exceptionoccurs in the try block. Typically, the statements of a finally blockrun when control leaves a try statement. The transfer of control canoccur as a result of normal execution, of execution of a break,continue, goto, or return statement, or of propagation of an exceptionout of the try statement.

Within a handled exception, the associated finally block is guaranteedto be run. However, if the exception is unhandled, execution of thefinally block is dependent on how the exception unwind operation istriggered. That, in turn, is dependent on how your computer is set up.

Usually, when an unhandled exception ends an application, whether ornot the finally block is run is not important. However, if you havestatements in a finally block that must be run even in that situation,one solution is to add a catch block to the try-finally statement.Alternatively, you can catch the exception that might be thrown in thetry block of a try-finally statement higher up the call stack. Thatis, you can catch the exception in the method that calls the methodthat contains the try-finally statement, or in the method that callsthat method, or in any method in the call stack. If the exception isnot caught, execution of the finally block depends on whether theoperating system chooses to trigger an exception unwind operation.

最后

当使用任何支持IDisposable 接口(interface)(旨在释放非托管资源)的东西时,您可以将其包装在using 中。陈述。编译器将生成一个 try {} finally {} 并在对象上内部调用 Dispose()

关于c# - 无法访问的代码,但可以异常访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55116849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com