我正在阅读受约束的执行区域和其他勘误表 [Brian Grunkemeyer],试图了解受约束的执行区域,但是我在理解以下示例时遇到了一些问题:
RuntimeHelpers.PrepareConstrainedRegions();
try {
// Prepare my backout code
MethodInfo m = _list.GetType().GetMethod("RemoveAt", new Type[] { typeof(int) });
RuntimeHelpers.PrepareMethod(m.MethodHandle);
IEnumerator en = c.GetEnumerator();
while(en.MoveNext()) {
_list.Insert(index++, en.Current);
// Assuming that these lines aren't reordered.
numAdded++;
}
_version++;
}
catch(Exception) {
// Reliable backout code
while(numAdded > 0) {
_list.RemoveAt(index--);
numAdded--;
}
throw;
}
我的理解是该try
块不受约束,只有 finally 和 catch 块受到约束。这意味着在try
阻塞期间可以随时抛出异步异常(例如 ThreadAbortException),特别是它可以在之前numAdded++
但之后抛出_list.Insert
。在这种情况下,退出代码会从_list
.
鉴于此,我很难理解此示例中受约束的执行区域的用途。
我对此的理解是正确的还是我错过了什么?