Search code examples
cexceptionseh

Catching opcode 0xCC as an exception


Say a C program might trigger an exception of opcode 0xCC
How can I catch it?

I tried:

__try   
    {

  ...some code

    }

__except(GetExceptionCode()==EXCEPTION_EXECUTE_HANDLER)     
{ 
     _tprintf(_T("Error\n"),i);

      return 0;
}

This is not working for me. What am I doing wrong? Thanks!


Solution

  • You're not checking for the right exception code.

    int 3 throws EXCEPTION_SINGLE_STEP.

    You handle it this way :

    __try 
    {
        // some code that might cause an int3
    }
    __except(GetExceptionCode() == EXCEPTION_SINGLE_STEP ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) 
    {
        // error handling of int3
    }
    

    EDIT: Please note that a code running with a debugger attached will not see the exception, because the debugger handles it and clears it before passing the hand back to the code.