无法访问的代码,但是有异常可以访问 [英] Unreachable code, but reachable with an exception

查看:135
本文介绍了无法访问的代码,但是有异常可以访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()记录为抛出无效的操作异常当方法调用对该对象的当前状态无效时。因此,如果发生这种情况, try 块的执行将停止,最终块将被执行,然后执行 return false; 在底部。

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.

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

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?

推荐答案

编译器警告(等级2)CS0162


检测到无法访问的代码

Unreachable code detected

编译器检测到永远不会执行的代码。

The compiler detected code that will never be executed.

这只是说 Compiler 足够理解通过 Statistic Analysis (静态分析),它无法达到并完全从已编译的 IL (发出警告)

Which is just saying, the Compiler understands enough through Static Analysis that it cant be reached and completely omits it from the compiled IL (hence your warning)

注意:您可以证明这一事实通过尝试使用调试器或使用IL资源管理器进入无法访问的代码来达到自己的目的

Note : You can prove this fact to your self by trying to Step on to the Unreachable Code with the debugger, or using an IL Explorer

最终可能在 Exception 上运行,(尽管除外)不会更改事实(在这种情况下),它仍然是未捕获的异常。如此,最后一个返回永远不会受到打击。

The finally may run on an Exception, (though that aside) it doesn't change the fact (in this case) it will still be an Uncaught Exception. Ergo, the last return will never get hit regardless.


  • 如果您希望代码继续到最后的返回,您唯一的选择是 catch Exception ;

  • If you want the code to continue onto the last return, your only option is to Catch the Exception;

如果不这样做,就将其保留并删除返回

If you don't, just leave it the way it is and remove the return.

示例

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

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

return false;






引用文档

尝试最终(C#参考)


通过使用finally块,您可以清理任何资源在try块中分配的
,即使try块中发生了异常
,您也可以运行代码。通常,当控制离开try语句时,finally块
的语句运行。正常执行,执行中断,执行
,继续执行goto或return语句或从try语句中传播异常
都会导致
的控制权转移

By using a finally block, you can clean up any resources that are allocated in a try block, and you can run code even if an exception occurs in the try block. Typically, the statements of a finally block run when control leaves a try statement. The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

在已处理的异常中,可以保证关联的finally块可以运行
但是,如果未处理异常,则
finally块的执行取决于
触发异常展开操作的方式。反过来,这取决于计算机的设置。

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

通常,当未处理的异常终止应用程序时,无论是
还是
不是finally块运行并不重要。 但是,如果即使在这种情况下,在finally块中也必须运行
语句,则
的一种解决方案是在try-finally语句中添加catch块

或者,您可以捕获在调用堆栈上方的try-finally语句的
try块中可能引发的异常

是,您可以在调用包含try-finally语句的方法
的方法中,或在调用
该方法的方法中,或在该方法中捕获异常。调用堆栈。如果未捕获到异常
,则执行finally块取决于
操作系统是否选择触发异常展开操作。

Usually, when an unhandled exception ends an application, whether or not the finally block is run is not important. However, if you have statements 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 the try block of a try-finally statement higher up the call stack. That is, you can catch the exception in the method that calls the method that contains the try-finally statement, or in the method that calls that method, or in any method in the call stack. If the exception is not caught, execution of the finally block depends on whether the operating system chooses to trigger an exception unwind operation.

最后

在使用支持 IDisposable 的任何内容时界面(旨在释放非托管资源),您可以将其包装在 使用 语句。编译器将最终生成 try {} {} 并在对象上内部调用 Dispose()

When using anything that supports the IDisposable interface (which is designed to release unmanaged resources), you can wrap it in a using statement. The compiler will generate a try {} finally {} and internally call Dispose() on the object

这篇关于无法访问的代码,但是有异常可以访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆