다음을 통해 공유


How to: Catch Exceptions in Native Code Thrown from MSIL

In native code, you can catch native C++ exception from MSIL. You can catch CLR exceptions with __try and __except.

For more information, see Structured Exception Handling (C/C++) and C++ Exception Handling.

Example

The following sample defines a module with two functions, one that throws a native exception, and another that throws an MSIL exception.

// catch_MSIL_in_native.cpp
// compile with: /clr /c
void Test() {
   throw ("error");
}

void Test2() {
   throw (gcnew System::Exception("error2"));
}

The following sample defines a module that catches a native and MSIL exception.

// 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();
}
error
caught an exception

See Also

Other Resources

Exception Handling (C++ Component Extensions)