Share via


/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");
    }
}

出力

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

関連付け解除再スロー

/clr は、キャッチ ハンドラーの外で例外を再スローすること (関連付け解除再スローと呼ばれます) をサポートしていません。 この種の例外は、標準の C++ 再スローとして扱われます。 アクティブなマネージド例外があるときに関連付け解除再スローが発生した場合、その例外は C++ 例外としてラップされてから再スローされます。 この種の例外は 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" );
    }
}

出力

caught an SEH Exception

例外フィルターと EXCEPTION_CONTINUE_EXECUTION

マネージド アプリケーションでフィルターから EXCEPTION_CONTINUE_EXECUTION が返された場合、フィルターから EXCEPTION_CONTINUE_SEARCH が返された場合と同様に扱われます。 これらの定数の詳細については、「try-except ステートメント」を参照してください。

この相違点を次の例で示します。

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

出力

Counter=-3

set_se_translator 関数

_set_se_translator の呼び出しによって設定される 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();
}

出力

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
MSVC での例外処理