Gewusst wie: Abfangen von Ausnahmen in systemeigenem Code ab, die von MSIL ausgelöst wird
In systemeigenem Code können Sie die systemeigene C++-Ausnahme von MSIL abfangen. Sie können CLR-Ausnahmen mit __try und __except abfangen.
Weitere Informationen finden Sie unter Strukturierte Ausnahmebehandlung (C/C++) und C++-Ausnahmebehandlung.
Beispiel
Das folgende Beispiel definiert ein Modul mit zwei Features, eine, die eine systemeigene Ausnahme auslöst, und eine weitere, die eine MSIL-Ausnahme auslöst.
// catch_MSIL_in_native.cpp
// compile with: /clr /c
void Test() {
throw ("error");
}
void Test2() {
throw (gcnew System::Exception("error2"));
}
Das folgende Beispiel definiert ein Modul, das einen systemeigenen und MSIL-Ausnahme abfängt.
// 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();
}