다음을 통해 공유


방법: 예외 Catch MSIL에서 발생 하는 네이티브 코드에서

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

자세한 내용은 구조적 예외 처리 (C/C++)C++ 예외 처리를 참조하십시오.

예제

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();
}
  

참고 항목

기타 리소스

예외를 /clr을 처리 합니다.