Compilerfehler C2715

'Typ' : Auslösen oder Abfangen dieses Typs nicht möglich

Werttypen sind keine gültigen Argumente für Ausnahmebehandlungen in verwaltetem Code (weitere Informationen unter Exception Handling under /clr).

// C2715a.cpp
// compile with: /clr
using namespace System;

value struct V {
   int i;
};

void f1() {
   V v;
   v.i = 10;
   throw v;   // C2715
   // try the following line instead
   // throw ((V^)v);
}

int main() {
   try {
      f1();
   }

   catch(V v) { if ( v.i == 10 ) {   // C2715
   // try the following line instead
   // catch(V^ pv) { if ( pv->i == 10 ) {
         Console::WriteLine("caught 10 - looks OK");
      } 
      else {
         Console::WriteLine("catch looks bad");
      }
   }
   catch(...) {
      Console::WriteLine("catch looks REALLY bad");
   }
}

Bei Verwendung der Ausnahmebehandlung in Managed Extensions for C++ sind __value-Typen oder __gc-Zeiger keine gültigen Argumente. Um diesen Fehler zu beheben, verwenden Sie das Schlüsselwort __box, um das Argument mittels Boxing zu packen.

Im folgenden Beispiel wird C2715 generiert:

// C2715b.cpp
// compile with: /clr:oldSyntax
using namespace System;

__value struct V {
   int i;
};

void f1() {
   V v;
   v.i = 10;
   throw v;   // C2715
   // try the following line instead
   // throw __box(v);
}

int main() {
   try {
      f1();
   }

   catch(V v) { if ( v.i == 10 ) {   // C2715
   // try the following line instead
   // catch(__box V *pv) { if ( pv->i == 10 ) {
         Console::WriteLine(S"caught 10 - looks OK");
      } 
      else {
         Console::WriteLine(S"catch looks bad");
      }
   }
   catch(...) {
      Console::WriteLine(S"catch looks REALLY bad");
   }
}