对于将System.Object作为类型过滤器的一般catch子句的任何实际影响?

Ste*_*sen 11 .net c# cil

我记得曾经听说过抛出某种类型的物体System.Exception(或延伸它的那些物体)在技术上是合法的CIL,尽管C#没有支持它的功能.所以我有兴趣看到以下C#代码:

try {
    throw new Exception();
} catch(Exception x) {
    try {
        throw;
    } catch {
        Console.Write("yes");
    }
}
Run Code Online (Sandbox Code Playgroud)

编译为以下CIL:

  .try
  {
    IL_0000:  newobj     instance void [mscorlib]System.Exception::.ctor()
    IL_0005:  throw
  }  // end .try
  catch [mscorlib]System.Exception 
  {
    IL_0006:  pop
    .try
    {
      IL_0007:  rethrow
    }  // end .try
    catch [mscorlib]System.Object 
    {
      IL_0009:  pop
      IL_000a:  ldstr      "yes"
      IL_000f:  call       void [mscorlib]System.Console::Write(string)
      IL_0014:  leave.s    IL_0016
    }  // end handler
    IL_0016:  leave.s    IL_0018
  }  // end handler
Run Code Online (Sandbox Code Playgroud)

我们看到嵌套的常规catch子句编译为:

catch [mscorlib]System.Object 
Run Code Online (Sandbox Code Playgroud)

在C#中,对于System.Object作为类型过滤器发出的一般catch子句而不是System.Exception

usr*_*usr 3

.NET-2.0 之前存在差异。我在 .NET 1.1 时代读到过它。

这里有解释(我就不复制了)。请注意,第一个答案是错误的,第二个答案是正确的。

至于它是否实用:不。我想这对于模糊的互操作场景很重要。