This documentation is archived and is not being maintained.


Microsoft Specific

This topic is new for Visual Studio 2005 SP1.

Generates an undefined instruction.

void __ud2();

The processor raises an invalid opcode exception if you execute an undefined instruction.

The __ud2 function is equivalent to the UD2 machine instruction, and is available only in kernel mode. For more information, search for the document, "Intel Architecture Software Developer's Manual, Volume 2: Instruction Set Reference," at the Intel Corporation site.

Intrinsic Architecture


x86, x64

Header file <intrin.h>

END Microsoft Specific

The following example executes an undefined instruction, which raises an exception. The exception handler then changes the return code from zero to one.

// __ud2_intrinsic.cpp
#include <stdio.h>
#include <intrin.h>
#include <excpt.h>
// compile with /EHa

int main() {

// Initialize the return code to 0.
 int ret = 0;

// Attempt to execute an undefined instruction.
  printf("Before __ud2(). Return code = %d.\n", ret);
  __try { 

// Catch any exceptions and set the return code to 1.
  printf("  In the exception handler.\n");
  ret = 1;

// Report the value of the return code. 
  printf("After __ud2().  Return code = %d.\n", ret);
  return ret;


Before __ud2(). Return code = 0.
  In the exception handler.
After __ud2().  Return code = 1.