c# - 为什么 CLR 会重新抛出 ThreadAbortException?

标签 c# multithreading clr threadabortexception

我从“Windows 上的并发编程”一书中获得了以下代码:

void Main()
{
    try
    {
        try
        {
            Console.WriteLine("Inside Main Method");
            Thread.CurrentThread.Abort();
        }
        catch(ThreadAbortException)
        {
            Console.WriteLine("Inside First Catch");
            // Trying to swallow but CLR throws it again....
        }
    }
    catch(ThreadAbortException)
    {
        Console.WriteLine("Inside Second Catch");
        //Thread.ResetAbort();
    }
}

我想知道为什么 CLR 会重新抛出 ThreadAbortException ?它一直这样做,直到我调用“Thread.ResetAbort()”。其次,是否有任何其他系统定义的异常得到 CLR 的特殊处理?

最佳答案

I am interested in knowing as why CLR re-throws the ThreadAbortException?

因为线程正在中止。人们一直在处理所有异常,即使这样做很危险。如果一个错误记录例程让一个本应被销毁的线程永远活着,那就太奇怪了,不是吗?

is there any other system defined exception, which gets special treatment from CLR?

是的,有几个。例如,堆栈外和内存外异常也有特殊的行为。

关于c# - 为什么 CLR 会重新抛出 ThreadAbortException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8237945/

相关文章:

c# - 为什么我在使用 IIS 时获得 Cryptography_CSP_NoPrivateKey 而在使用 VS 开发服务器时却没有?

c# - List<T>.ToList() 是否枚举集合

c++ - 多线程 (openMP) - 有多少并行线程

multithreading - 自旋锁是免费的吗?

Java多线程数据库访问

c# - 如何在 Visual Studio 中抑制程序集引用警告?

c# - 数值太小或误差太大

c# - 为什么 C# 4.0 的协变/逆变仅限于参数化接口(interface)和委托(delegate)类型?

c# - 转换和转换是一回事吗?

c# - MSIL : Comparing efficiency of simple algorithms