Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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++) and C++ Exception Handling.


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 {
   __except(1) {
      cout << "caught an exception" << endl;


int main() {
   // catch native C++ exception from MSIL
   try {
   catch(char * S) {
      cout << S << endl;


caught an exception

See Also

Community Additions

© 2015 Microsoft