Compiler Warning (level 1) CS1058

前一个 catch 子句已经捕获所有异常。引发的所有异常均被包装在 System.Runtime.CompilerServices.RuntimeWrappedException 中

如果 catch() 块在 catch (System.Exception e) 块后没有指定的异常类型,则该特性会导致 CS1058。该警告建议 catch() 块将不捕获任何异常。

如果在 AssemblyInfo.cs 文件中将 RuntimeCompatibilityAttribute 设置为 False,则 catch (System.Exception e) 块之后的 catch() 块可捕获非 CLS 异常:[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]。如果未将此特性显式设置为 False,则所有引发的非 CLS 异常将包装为异常,并且 catch (System.Exception e) 块将捕获它们。有关更多信息,请参见如何:捕捉非 CLS 异常

下面的示例生成 CS1058。

// CS1058.cs
// CS1058 expected
using System.Runtime.CompilerServices;

// the following attribute is set to true by default in the C# compiler
// set to false in your source code to resolve CS1058
[assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = true)]

class TestClass 
{
   static void Main() 
   {
      try {}

      catch (System.Exception e) { 
         System. Console.WriteLine("Caught exception {0}", e);
      }

      catch {}   // CS1058\. This line will never be reached.
   }
}