Compiler Warning (level 1) CS1058

 

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

A previous catch clause already catches all exceptions. All exceptions thrown will be wrapped in a System.Runtime.CompilerServices.RuntimeWrappedException

This attribute causes CS1058 if a catch() block has no specified exception type after a catch (System.Exception e) block. The warning advises that the catch() block will not catch any exceptions.

A catch() block after a catch (System.Exception e) block can catch non-CLS exceptions if the RuntimeCompatibilityAttribute is set to false in the AssemblyInfo.cs file: [assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]. If this attribute is not set explicitly to false, all thrown non-CLS exceptions are wrapped as Exceptions and the catch (System.Exception e) block catches them. For more information, see How to: Catch a non-CLS Exception.

The following example generates 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.  
   }  
}  

Show: