How to Implement Software Exception Handling


Challenge

Implement software exception handling for the Intel® C++ Compiler that is compliant with the Microsoft* Structured Exception Handling (SEH) implementation. There are, in general, two types of exceptions.

  1. Asynchronous exceptions: those exceptions that come unexpectedly, similar to an interrupt in a program that takes you immediately away from the thread context you are executing to a context that is different from that which caused the exception.
  2. Synchronous exceptions: those exceptions that are planned and handled in an organized manner. The most common type of synchronous exception is implemented as a "throw."

 

The following sample code shows synchronous exception handling:

int MyFunction()

{

int RetVal;

try

{

throw(ExceptionTypeX);

}

catch(int MyExpectedType1)

{

// handle 'int' exception

}

catch(SomeClassType MyExpectedType2)

{

// handle class exception

}

// etc

return RetVal;

}

 

The following sample code shows asynchronous exception handling:

try

{

//Instruction that generates an exception

return true;

}

catch(...)

{

return false;

}

}

 

ISO standard exception handling is a set of entry points provided to the user to handle predefined conditions. These entry points consist of the following:

  • catch -
  • throw -
  • try -

 

A function defines which exceptions it raises with an exception specification:

throw (MyExceptionType)

 

The exception type may be one or more type names separated by commas. Handlers within try blocks catch the corresponding exceptions.

Microsoft SEH is a set of Windows* functions provided by the operating system. Microsoft Visual C++* abstracts these functions via non-standard keywords and library routines. Other vendors may choose different ways to achieve similar results by calling the same functions. To support SEH, Microsoft extends the C and C++ languages with four keywords:

  • __except -
  • __finally -
  • __leave -
  • __try -

 

A function defines which exceptions it raises with an exception specification:

throw (MyExceptionTypes)

 

The exception types list contains one or more type names, separated by commas. Handlers that are organized within try blocks catch the exceptions. The difference between the Microsoft extensions and the ISO standard is the way these mechanisms handle the exception.


Solution

Use the switch for asynchronous exception handling. Microsoft can customize their exception handling to a specific compiler and operating system, so the way it is handled can be implemented at the operating system (OS) level. This makes their method more efficient and eliminates the need to duplicate the exception raising and handling software in each individual piece of code. Rather, the C/C++ runtime can handle it. The ISO C++ exception-handling standard, on the other hand, is compiler- and OS-agnostic. Therefore, it needs to compile and link in extra code into each binary that provides software exception handling.

Although the Intel C++ compiler provides the functionality of both the SEH and ISO exception-handling model, you must make sure that the compiler switch for asynchronous exception handling is on. Otherwise, the following code (for example) compiles without warnings on the Intel Compiler using the Microsoft Exception model:

w__try

{

//Instruction that generates an exception

return true;

}

__except(1)

{

return false;

}

}

 

If you do not use the asynchronous exception switch for the compiler, it will not catch the exception in the except statement and will be left as an exception for the r untime to handle, causing the program to exit. However, if you were to implement the code as follows, using the ISO exception model, it will add extra code within your program to handle the exception (bypassing the runtime).

try

{

//Instruction that generates an exception

return true;

}

catch(?)

{

return false;

}

}

 


Source

"Software Exception Handling" by Rich Winterton.


Categories:
Tags:
For more complete information about compiler optimizations, see our Optimization Notice.

Comments

's picture

This page states that "If you do not use the asynchronous exception switch for the compiler, it will not catch the exception", however I have spent 2 hours looking for the asynchronous exception compiler switch, and the only thing I've found is "-fasynchoronous-unwind-tables" on this site http://patchwork.ozlabs.org/patch/62015. When I added that to my CFLAGS, it was unknown by the Intel C++ compiler.

Please add the actual compiler switch values to this page so that engineers don't have to search for hours for something that should be easy to find. It's taking hours to sort out this little problem.

http://software.intel.com/en-us/articles/how-to-implement-software-exception-handling/

Jim Roedder