与“异常如何在 C++ 中工作(在幕后) ”相同,但适用于C#。
我知道抛出异常时必须执行以下步骤。
- 为异常类型找到最近的处理程序;
- 将堆栈展开到处理程序级别;
- 调用处理程序;
- 查找并调用每个
finally
块。
.NET 如何处理这些操作?“当前”处理程序的映射如何工作?在 try/catch 块中发出了多少代码?在投掷块中?
Windows 上的 .NET 异常使用操作系统的底层结构化异常处理(SEH) 机制,其方式与本机代码相同。如C (and C++)的链接问题中所列。
.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.