20

与“异常如何在 C++ 中工作(在幕后) ”相同,但适用于C#

我知道抛出异常时必须执行以下步骤。

  1. 为异常类型找到最近的处理程序;
  2. 将堆栈展开到处理程序级别;
  3. 调用处理程序;
  4. 查找并调用每个finally块。

.NET 如何处理这些操作?“当前”处理程序的映射如何工作?在 try/catch 块中发出了多少代码?在投掷块中?

4

2 回答 2

4

Windows 上的 .NET 异常使用操作系统的底层结构化异常处理(SEH) 机制,其方式与本机代码相同。如C (and C++)的链接问题中所列。

于 2010-09-21T14:47:43.830 回答
3

.NET exceptions use the underlying Windows structured exception handling implementation, though this is not a requirement. Mono may do it differently.

In fact, if you write a single-line Console app that just throws an exception, and then run it in Windbg, you'll see the hook into the unmanaged exception handling.

于 2010-09-21T14:51:17.460 回答