Partager via


C6312

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

warning C6312: Possible infinite loop: use of the constant EXCEPTION_CONTINUE_EXECUTION in the exception-filter expression of a try-except

This warning indicates the use of the constant EXCEPTION_CONTINUE_EXECUTION (or another constant that evaluates to -1) in the filter expression of a structured exception handler. Use of the constant value EXCEPTION_CONTINUE_EXECUTION could lead to an infinite loop. For example, if an exception was raised by hardware, the instruction that caused the exception will be restarted. If the address that caused the exception is still bad, another exception will occur and be handled in the same way. This causes an infinite loop.

An explicit call to RaiseException will not directly cause an infinite loop, but it will continue execution of the code in the protected block. This can be unexpected, and could lead to an infinite loop if RaiseException was used to avoid dereferencing an invalid pointer.

Typically, EXCEPTION_CONTINUE_EXECUTION should be returned only by a function called in the filter expression, which has a chance to fix either the pointer that caused the exception or the underlying memory.

Example

The following code generates this warning:

#include <excpt.h>  
#include <stdio.h>  
#include <windows.h>  
  
void f (char *ptr)  
{  
  __try   
  {  
    // exception occurs if the caller passes null ptr  
    // code...     
   *ptr = '\0';  
  }   
  __except (EXCEPTION_CONTINUE_EXECUTION)  
    // When EXCEPTION_CONTINUE_EXECUTION is used, the handler  
    //  block of the structured exception handler is not executed.   
  {  
    puts("This block is never executed");  
  }  
}  

To correct this warning, use the following code:

#include <excpt.h>  
#include <stdio.h>  
#include <windows.h>  
  
void f (char *ptr)  
{  
  __try   
  {  
    // exception occurs if the caller passes null ptr  
    // code...  
    *ptr = '\0';  
  }   
  __except (GetExceptionCode()== EXCEPTION_ACCESS_VIOLATION ?   
               EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)   
  {  
    puts("Error Occurred");  
  }  
}