Talk:Filtering Structured Exceptions (C++)

From RAD Studio
Jump to: navigation, search

Related code example (from Bruneau):

#pragma hdrstop
#include <tchar.h>
#include <stdio.h>
// ---------------------------------------------------------------------------
class TAirbus {
private:
      int capacity;
public:
      int getCapacity() {
            return capacity;
      }
};
 
static unsigned int exception_code = 0;
static int filter(unsigned int code, struct _EXCEPTION_POINTERS* /*ep*/)
{
  exception_code = code;
  return EXCEPTION_EXECUTE_HANDLER;
}
 
int _tmain(int argc, _TCHAR* argv[]) 
{
  __try
  {
    TAirbus *airbus = 0;
    printf("Airbus capacity is %d\n", airbus->getCapacity()); // application block
    return 0;
  }
  __except(filter(GetExceptionCode(), GetExceptionInformation()))
  {
    char msg[256];
    msg[0] = 0;
    const char* pmsg;
    switch(exception_code)
    {
      case EXCEPTION_ACCESS_VIOLATION:
        pmsg = "EXCEPTION_ACCESS_VIOLATION";
        break;
      case STATUS_FLOAT_INVALID_OPERATION:
        pmsg = "STATUS_FLOAT_INVALID_OPERATION";
        break;
      // etc, etc - Handle other exception code here
      default:
      {
        sprintf(msg, "ExceptionCode:%lX", exception_code);
        pmsg = msg;
      }  
    }
    printf("%s\n", pmsg);
  }
}

Vadimb 04:52, 18 July 2011 (PDT)