272

我在另一个问题中看到了这个提示,想知道是否有人可以向我解释这到底是如何工作的?

try { return x; } finally { x = null; }

我的意思是,finally子句真的在语句之后执行吗?return这段代码有多线程不安全?你能想到任何其他可以通过这个try-finallyhack 完成的黑客行为吗?

4

5 回答 5

376

finally 语句被执行,但返回值不受影响。执行顺序为:

  1. 执行 return 语句之前的代码
  2. return 语句中的表达式被评估
  3. finally 块被执行
  4. 返回步骤 2 中评估的结果

这是一个简短的程序来演示:

using System;

class Test
{
    static string x;

    static void Main()
    {
        Console.WriteLine(Method());
        Console.WriteLine(x);
    }

    static string Method()
    {
        try
        {
            x = "try";
            return x;
        }
        finally
        {
            x = "finally";
        }
    }
}

这会打印“try”(因为这是返回的内容),然后是“finally”,因为这是 x 的新值。

当然,如果我们返回一个对可变对象(例如 StringBuilder)的引用,那么在 finally 块中对对象所做的任何更改都将在返回时可见——这并没有影响返回值本身(这只是一个参考)。

于 2009-01-07T19:53:56.940 回答
245

不 - 在 IL 级别,您不能从异常处理块内部返回。它本质上将其存储在一个变量中,然后返回

即类似于:

int tmp;
try {
  tmp = ...
} finally {
  ...
}
return tmp;

例如(使用反射器):

static int Test() {
    try {
        return SomeNumber();
    } finally {
        Foo();
    }
}

编译为:

.method private hidebysig static int32 Test() cil managed
{
    .maxstack 1
    .locals init (
        [0] int32 CS$1$0000)
    L_0000: call int32 Program::SomeNumber()
    L_0005: stloc.0 
    L_0006: leave.s L_000e
    L_0008: call void Program::Foo()
    L_000d: endfinally 
    L_000e: ldloc.0 
    L_000f: ret 
    .try L_0000 to L_0008 finally handler L_0008 to L_000e
}

这基本上声明了一个局部变量(CS$1$0000),将值放入变量中(在处理的块内),然后在退出块后加载变量,然后返回它。反射器将其呈现为:

private static int Test()
{
    int CS$1$0000;
    try
    {
        CS$1$0000 = SomeNumber();
    }
    finally
    {
        Foo();
    }
    return CS$1$0000;
}
于 2009-01-07T19:50:20.037 回答
22

finally 子句在 return 语句之后但在从函数实际返回之前执行。我认为这与线程安全无关。这不是 hack - finally 保证始终运行,无论您在 try 块或 catch 块中做什么。

于 2009-01-07T19:49:44.750 回答
13

除了 Marc Gravell 和 Jon Skeet 给出的答案之外,重要的是要注意对象和其他引用类型在返回时的行为相似,但确实存在一些差异。

返回的“What”遵循与简单类型相同的逻辑:

class Test {
    public static Exception AnException() {
        Exception ex = new Exception("Me");
        try {
            return ex;
        } finally {
            // Reference unchanged, Local variable changed
            ex = new Exception("Not Me");
        }
    }
}

在finally 块中为局部变量分配新引用之前,已经评估了正在返回的引用。

执行本质上是:

class Test {
    public static Exception AnException() {
        Exception ex = new Exception("Me");
        Exception CS$1$0000 = null;
        try {
            CS$1$0000 = ex;
        } finally {
            // Reference unchanged, Local variable changed
            ex = new Exception("Not Me");
        }
        return CS$1$0000;
    }
}

不同之处在于仍然可以使用对象的属性/方法来修改可变类型,如果您不小心,可能会导致意外行为。

class Test2 {
    public static System.IO.MemoryStream BadStream(byte[] buffer) {
        System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);
        try {
            return ms;
        } finally {
            // Reference unchanged, Referenced Object changed
            ms.Dispose();
        }
    }
}

关于 try-return-finally 要考虑的第二件事是“通过引用”传递的参数在返回后仍然可以修改。只有返回值已被评估并存储在等待返回的临时变量中,任何其他变量仍以正常方式修改。out 参数的合约甚至可以在 finally 块以这种方式之前无法实现。

class ByRefTests {
    public static int One(out int i) {
        try {
            i = 1;
            return i;
        } finally {
            // Return value unchanged, Store new value referenced variable
            i = 1000;
        }
    }

    public static int Two(ref int i) {
        try {
            i = 2;
            return i;
        } finally {
            // Return value unchanged, Store new value referenced variable
            i = 2000;
        }
    }

    public static int Three(out int i) {
        try {
            return 3;
        } finally {
            // This is not a compile error!
            // Return value unchanged, Store new value referenced variable
            i = 3000;
        }
    }
}

像任何其他流结构一样,“try-return-finally”有它的位置,可以让代码看起来更简洁,而不是编写它实际编译到的结构。但必须小心使用它以避免陷阱。

于 2012-07-12T17:54:45.827 回答
4

ifx是一个局部变量,我看不到重点, asx无论如何都会在方法退出并且返回值的值不为 null 时有效设置为 null (因为它是在调用 set 之前放置在寄存器中的x为空)。

如果您想保证在返回时(以及在确定返回值之后)更改字段的值,我只能看到这样做。

于 2009-01-07T19:53:51.183 回答