Dmi*_*ruk 253 .net c# exception-handling
我在另一个问题中看到了这个提示,并想知道是否有人可以向我解释这是如何工作的?
try { return x; } finally { x = null; }
Run Code Online (Sandbox Code Playgroud)
我的意思是,该finally条款真正执行后的return声明?这段代码的线程不安全吗?你能想到这个try-finally黑客可以做的任何额外的hackery 吗?
Jon*_*eet 351
执行finally语句,但返回值不受影响.执行顺序是:
这是一个简短的程序来演示:
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";
}
}
}
Run Code Online (Sandbox Code Playgroud)
这打印"尝试"(因为这是返回的内容)然后"最后",因为这是x的新值.
当然,如果我们返回对可变对象(例如StringBuilder)的引用,那么对finally块中的对象所做的任何更改都将在返回时可见 - 这不会影响返回值本身(这只是一个参考).
Mar*_*ell 230
否 - 在IL级别,您无法从异常处理的块内返回.它基本上将它存储在一个变量中,然后返回
即类似于:
int tmp;
try {
tmp = ...
} finally {
...
}
return tmp;
Run Code Online (Sandbox Code Playgroud)
例如(使用反射器):
static int Test() {
try {
return SomeNumber();
} finally {
Foo();
}
}
Run Code Online (Sandbox Code Playgroud)
编译为:
.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
}
Run Code Online (Sandbox Code Playgroud)
这基本上声明了一个局部变量(CS$1$0000),将值放入变量(在处理块内),然后在退出块后加载变量,然后返回它.Reflector将其渲染为:
private static int Test()
{
int CS$1$0000;
try
{
CS$1$0000 = SomeNumber();
}
finally
{
Foo();
}
return CS$1$0000;
}
Run Code Online (Sandbox Code Playgroud)
Otá*_*cio 19
finally子句在return语句之后但在从函数实际返回之前执行.我认为它与线程安全无关.它不是一个黑客 - 无论你在try块或catch块中做什么,最终都能保证始终运行.
Ark*_*e55 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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
在为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;
}
}
Run Code Online (Sandbox Code Playgroud)
区别在于仍然可以使用对象的属性/方法修改可变类型,如果不小心,可能会导致意外行为.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
关于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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
像任何其他流构造一样,"try-return-finally"有它的位置,并且可以允许比编写实际编译的结构更清晰的代码.但必须谨慎使用以避免陷阱.
| 归档时间: |
|
| 查看次数: |
73263 次 |
| 最近记录: |