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
Expand Minimize
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.

Compiler Error C2707

'identifier' : bad context for intrinsic function

Structured exception-handling intrinsics are invalid in certain contexts:

  • _exception_code() outside an exception filter or __except block

  • _exception_info() outside an exception filter

  • _abnormal_termination() outside a __finally block

To resolve the error, be sure that the exception-handling intrinsics are placed in the appropriate context.

The following sample generates C2707.

// C2707.cpp
#include <windows.h>
#include <stdio.h>

LONG MyFilter(LONG excode) 
{
    return (excode == EXCEPTION_ACCESS_VIOLATION ?
        EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH);   // OK
}

LONG func(void) 
{
    int x, y;
    return(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?  // C2707
             EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH);

    __try 
    {
        y = 0;
        x = 4 / y;
        return 0;
     }

    __except(MyFilter(GetExceptionCode())) 
    {
        return(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? // ok
               EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH);
    }
}

int main() 
{
    __try 
    {
        func();
    } __except(EXCEPTION_EXECUTE_HANDLER)
    {
        printf_s("Caught exception\n");
    }
}

Community Additions

ADD
Show:
© 2015 Microsoft