Cómo: Excepciones de filtrado en código nativo que produce MSIL
En código nativo, puede detectar excepciones de C++ nativo de MSIL.Puede detectar las excepciones de CLR con __try y __except.
Para obtener más información, vea Control de excepciones estructurado (C++) y Control de excepciones de C++.
Ejemplo
El ejemplo siguiente se define un módulo con dos funciones, una que produce una excepción nativa, y otra que produce una excepción de MSIL.
// catch_MSIL_in_native.cpp
// compile with: /clr /c
void Test() {
throw ("error");
}
void Test2() {
throw (gcnew System::Exception("error2"));
}
El ejemplo siguiente define un módulo que detecte un natural y una excepción de MSIL.
// catch_MSIL_in_native_2.cpp
// compile with: /clr catch_MSIL_in_native.obj
#include <iostream>
using namespace std;
void Test();
void Test2();
void Func() {
// catch any exception from MSIL
// should not catch Visual C++ exceptions like this
// runtime may not destroy the object thrown
__try {
Test2();
}
__except(1) {
cout << "caught an exception" << endl;
}
}
int main() {
// catch native C++ exception from MSIL
try {
Test();
}
catch(char * S) {
cout << S << endl;
}
Func();
}