Partager via


Avertissement du compilateur (niveau 1) CS1058

Mise à jour : novembre 2007

Message d'erreur

Une clause catch précédente intercepte déjà toutes les exceptions. Toutes les exceptions levées seront encapsulées dans un System.Runtime.CompilerServices.RuntimeWrappedException
A previous catch clause already catches all exceptions. All exceptions thrown will be wrapped in a System.Runtime.CompilerServices.RuntimeWrappedException

Cet attribut provoque l'avertissement CS1058 si un bloc catch() n'a aucun type d'exception spécifié après un bloc catch (System.Exception e). L'avertissement recommande que le bloc catch() n'intercepte pas les exceptions.

Un bloc catch() après un bloc catch (System.Exception e) peut intercepter des exceptions non conformes à CLS si RuntimeCompatibilityAttribute a la valeur false dans le fichier AssemblyInfo.cs : [assembly: RuntimeCompatibilityAttribute(WrapNonExceptionThrows = false)]. Si cet attribut n'est pas défini explicitement avec la valeur false, toutes les exceptions levées non conformes à CLS sont encapsulées comme Exceptions et le bloc catch (System.Exception e) les intercepte. Pour plus d'informations, consultez Comment : intercepter une exception non-CLS.

Exemple

L'exemple suivant génère l'erreur 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.
   }
}