Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
80 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

  Contents

 Termination
 Handlers

 Performance
 and Flexibility


 Comment on
 this article

 Printable version

 


Performance and Flexibility

Exception Filters

C++ directly supports exception filters, in the form of type-checking. Consider the following example.

Examples


// These "empty" types allow us to filter exceptions - typically defined elsewhere
struct exception {}; // anything
struct file_exception : public exception {}; // all file exceptions
struct eof : public file_exception {};
struct file_not_found : public file_exception {};
struct unsupported_seek : public file_exception {};
struct out_of_memory : public exception {};

// Function declarations for example
void do_something() throw( eof, file_not_found, unsupported_seek );
void do_something_else() throw( out_of_memory );

void some_complex_function()
{
  try
  {
    do_something();
    do_something_else();
  }

  catch( eof ) {} // catches end of file exceptions
  catch( file_not_found ) {} // catches file not found exceptions
  catch( file_exception ) {} // catches all other file exceptions
  catch( exception ) {} // catches anything else
}

C++ EH's type-safety also has a notable advantage over SEH. C++ EH can use any type for filtering, while SEH is restricted to packing values into an unsigned int in order to emulate a more primitive form of type-checking. Why is this important? Well, duplicate types are not allowed by the compiler in C++ EH, but since SEH uses only an unsigned int value, its value might conflict with exceptions defined by other code.

C++ EH protects C++ from incorrectly handling code written by other people, since the unknown exception will simply keep unwinding the stack until it is either handled by someone who actually _knows_ the type, or the program ends.

With SEH, if two exception constants have the same value, it is possible for client code to throw an exception and your code to misinterpret it. Chances are that 100 years down the road, your grandchildren's X-Plus code will catch and interpret the exception differently than was intended. ;)

Performance

Although C++ EH is supposed to be slower than SEH, I do not understand why that matters in this comparison. EH is used to handle exceptional conditions, and is not intended to be used as a replacement for assertions, logging, or return values in time-critical code.

Simplicity

Curiosly enough, this category was not introduced in the comparison. C++ EH requires 3 new keywords, while SEH requires that plus learning a new (small) API.

Any additional conditional logic needed to handle the exception (or simply rethrow it) can and should be written _inside_ the appropriate catch block. The C++ EH conditional logic is exactly the same as normal (i.e., if...else, etc.), while SEH depends on API functions and specialized code, and precludes the use of normal "flow of execution modifiers" unless you want to lose the performance "advantage."

Portability

C++ EH is portable across all platforms that support the C++ standard. SEH is portable across all languages that Windows supports and compiler-writers comply with. I prefer the former.

Conclusion to the Rebuttal

If you use C++, then C++ EH is probably the better choice. Of course, that is coming from a C++ user, so it is immediately invalidated. ;) (j/k)

- null_pointer (null_pointer_us@yahoo.com)