/CLR における例外処理動作の相違点

マネージ例外の基本概念 は、マネージ アプリケーションで例外処理について説明します。このトピックでは、例外処理の標準の動作の違いと、いくつかの制限が詳しく説明します。詳細については、 _set_se_translator 関数を参照してください。

finally ブロックの外部にジャンプする。

ネイティブ C/C++ コードでは、構造化例外処理 (SEH) を使用してのfinally ブロックの外部にジャンプすることは警告が生成するが許可されます。/clrの下で、 finally ブロックの外部にジャンプすると、エラーが発生します:

// clr_exception_handling_4.cpp
// compile with: /clr
int main() {
   try {}
   finally {
      return 0;   // also fails with goto, break, continue
    }
}   // C3276

例外のフィルター内の例外の発生

例外がマネージ コード内の 例外のフィルター の処理中に発生すると、例外は、フィルターの戻り 0 キャッチおよび処理されます。

GetExceptionInformationによって返される)ため、入れ子になった例外が発生したネイティブ コードの動作とは対照的に、 EXCEPTION_RECORD の構造体のフィールド ExceptionRecord 設定されます ExceptionFlags 0x10 ビット フィールド セットです。次の例は、動作の違いを示します。:

// clr_exception_handling_5.cpp
#include <windows.h>
#include <stdio.h>
#include <assert.h>

#ifndef false
#define false 0
#endif

int *p;

int filter(PEXCEPTION_POINTERS ExceptionPointers) {
   PEXCEPTION_RECORD ExceptionRecord = 
                     ExceptionPointers->ExceptionRecord;

   if ((ExceptionRecord->ExceptionFlags & 0x10) == 0) {
      // not a nested exception, throw one
      *p = 0; // throw another AV
   }
   else {
      printf("Caught a nested exception\n");
      return 1;
    }

   assert(false);

   return 0;
}

void f(void) {
   __try {
      *p = 0;   // throw an AV
   }
   __except(filter(GetExceptionInformation())) {
      printf_s("We should execute this handler if "
                 "compiled to native\n");
    }
}

int main() {
   __try {
      f();
   }
   __except(1) {
      printf_s("The handler in main caught the "
               "exception\n");
    }
}

2ww6y7y2.collapse_all(ja-jp,VS.110).gif出力

Caught a nested exception
We should execute this handler if compiled to native

引き離された Rethrows

/clr 、 catch ハンドラー外の例外がサポートされません (引き離された再スローすると呼ばれる)を rethrowing。この種類の例外は、標準 C++ の再として扱われます。次に実行中のマネージ例外がある場合にスロー引き離されたが検出された場合、例外は、. C++ 例外を再スローとしてラップされます。この種類の例外は、例外の種類 System::SEHExceptionとしてのみキャッチできません。

次の例は、. C++ 例外としてマネージ例外を再スローを次に示します:

// clr_exception_handling_6.cpp
// compile with: /clr
using namespace System;
#include <assert.h>
#include <stdio.h>

void rethrow( void ) {
   // This rethrow is a dissasociated rethrow.
   // The exception would be masked as SEHException.
   throw;
}

int main() {
   try {
      try {
         throw gcnew ApplicationException;
      }
      catch ( ApplicationException^ ) {
         rethrow();
         // If the call to rethrow() is replaced with
         // a throw statement within the catch handler,
         // the rethrow would be a managed rethrow and
         // the exception type would remain 
         // System::ApplicationException
      }
   }

    catch ( ApplicationException^ ) {
      assert( false );

      // This will not be executed since the exception
      // will be masked as SEHException.
    }
   catch ( Runtime::InteropServices::SEHException^ ) {
      printf_s("caught an SEH Exception\n" );
    }
}

2ww6y7y2.collapse_all(ja-jp,VS.110).gif出力

caught an SEH Exception

例外のフィルター処理と EXCEPTION_CONTINUE_EXECUTION

フィルターがマネージ アプリケーションの EXCEPTION_CONTINUE_EXECUTION を返す場合、フィルターが EXCEPTION_CONTINUE_SEARCHを返すように処理されます。これらの定数の詳細については、 ステートメントとを除くを参照してください。

次の例に、この違いを示します。

// clr_exception_handling_7.cpp
#include <windows.h>
#include <stdio.h>
#include <assert.h>

int main() {
   int Counter = 0;
   __try {
      __try  {
         Counter -= 1;
         RaiseException (0xe0000000|'seh',
                         0, 0, 0);
         Counter -= 2;
      }
      __except (Counter) {
         // Counter is negative,
         // indicating "CONTINUE EXECUTE"
         Counter -= 1;
      }
    }
    __except(1) {
      Counter -= 100;
   }

   printf_s("Counter=%d\n", Counter);
}

2ww6y7y2.collapse_all(ja-jp,VS.110).gif出力

Counter=-3

_set_se_translator 関数

変換関数、 _set_se_translator呼び出し、影響して設定はアンマネージ コードでのみキャッチします。次の例では、この制限を次に示します:

// clr_exception_handling_8.cpp
// compile with: /clr /EHa
#include <iostream>
#include <windows.h>
#include <eh.h>
#pragma warning (disable: 4101)
using namespace std;
using namespace System;

#define MYEXCEPTION_CODE 0xe0000101

class CMyException {
public:
   unsigned int m_ErrorCode;
   EXCEPTION_POINTERS * m_pExp;

   CMyException() : m_ErrorCode( 0 ), m_pExp( NULL ) {}

   CMyException( unsigned int i, EXCEPTION_POINTERS * pExp )
         : m_ErrorCode( i ), m_pExp( pExp ) {}

   CMyException( CMyException& c ) : m_ErrorCode( c.m_ErrorCode ),
                                      m_pExp( c.m_pExp ) {}

   friend ostream& operator << 
                 ( ostream& out, const CMyException& inst ) {
      return out <<  "CMyException[\n" <<  
             "Error Code: " << inst.m_ErrorCode <<  "]";
    }
};

#pragma unmanaged 
void my_trans_func( unsigned int u, PEXCEPTION_POINTERS pExp ) {
   cout <<  "In my_trans_func.\n";
   throw CMyException( u, pExp );
}

#pragma managed 
void managed_func() {
   try  {
      RaiseException( MYEXCEPTION_CODE, 0, 0, 0 );
   }
   catch ( CMyException x ) {}
   catch ( ... ) {
      printf_s("This is invoked since "
               "_set_se_translator is not "
               "supported when /clr is used\n" );
    }
}

#pragma unmanaged 
void unmanaged_func() {
   try  {
      RaiseException( MYEXCEPTION_CODE, 
                      0, 0, 0 );
   }
   catch ( CMyException x ) {
      printf("Caught an SEH exception with "
             "exception code: %x\n", x.m_ErrorCode );
    }
    catch ( ... ) {}
}

// #pragma managed 
int main( int argc, char ** argv ) {
   _set_se_translator( my_trans_func );

   // It does not matter whether the translator function
   // is registered in managed or unmanaged code
   managed_func();
   unmanaged_func();
}

2ww6y7y2.collapse_all(ja-jp,VS.110).gif出力

This is invoked since _set_se_translator is not supported when /clr is used
In my_trans_func.
Caught an SEH exception with exception code: e0000101

参照

関連項目

safe_cast (C++ コンポーネント拡張)

Visual C++ の例外処理

その他の技術情報

例外処理 (C++ コンポーネント拡張)