.net - On Error Resume Next 的 CIL 实现

标签 .net vb.net cil

我一直想知道:臭名昭著的 VB.NET 语句“On Error Resume Next”是如何转换为 CIL 的?它是否涉及用 try...catch 包装每一行?

最佳答案

转到。

您无法在一种方法中将 try-catch 与遗留错误处理结合起来。当您使用On Error Resume Next时,该方法中的每一行代码都会获得一个标签以及唯一标识符。在伪 C# 代码中:

int currentId = 0;

Label1:
currentId = 1;
Line1();

Label2:
currentId = 2;
Line2();

Label3:
currentId = 3;
Line3();

Exit:

整个方法被包装在 try catch 中。当发生异常时,catch 处理程序将检查 currentId 并对行中的下一个标签执行简单的 goto(并且设置 ProjectError)。在我们的例子中,它看起来像这样:

try
{
  ...
}
catch (Exception ex)
{
  ProjectData.SetProjectError(ex);

  if (currentId == 1) goto Label2;
  if (currentId == 2) goto Label3;
  if (currentId == 3) goto Exit;
}

请注意,这只是我通过反编译 VB.NET 应用程序获得的实现细节。唯一的契约行为是 On Error Resume Next 中定义的行为,它基本上可以归结为“设置项目错误并继续执行下一条语句”。

关于.net - On Error Resume Next 的 CIL 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40061329/

相关文章:

jquery 方法 : validate a div rather than a form

c# - 如何给c#中的进程赋予ctrl + c?

c# - 嵌套 View 模型未使用 DataAnnotations 从 .net 中的单元测试进行验证

c# - 哪个类用于 "Text Visualizer"?

c# - 为什么静态类被认为是 “classes” 和 “reference types” ?

c# - 在多个管道服务器实例的情况下异步 NamedPipes

.net - 按名称选择特定打印机 VB

c# - MSIL : Comparing efficiency of simple algorithms

.net - 验证 .NET Framework 程序集

vb.net - 为什么在构建发布时无法在 Windows 10 64 位中连接到 Postgresql?