Game Engine Architecture

(Ben Green) #1

132 3. Fundamentals of Software Engineering for Games


One way to solve this problem is to throw an exception. Structured excep-
tion handling (SEH) is a very powerful feature of C++. It allows the function
that detects a problem to communicate the error to the rest of the code with-
out knowing anything about which function might handle the error. When an
exception is thrown, relevant information about the error is placed into a data
object of the programmer’s choice known as an exception object. The call stack
is then automatically unwound, in search of a calling function that wrapped
its call in a try-catch block. If a try-catch block is found, the exception object
is matched against all possible catch blocks and if a match is found, the cor-
responding catch block’s code is executed. The destructors of any automatic
variables are called as needed during the stack unwinding.
The ability to separate error detection from error handling in such a clean
way is certainly att ractive, and exception handling is an excellent choice for
some soft ware projects. However, SEH adds a lot of overhead to the program.
Every stack frame must be augmented to contain additional information re-
quired by the stack unwinding process. Also, the stack unwind is usually very
slow—on the order of two to three times more expensive than simply return-
ing from the function. Also, if even one function in your program (or a library
that your program links with) uses SEH, your entire program must use SEH.
The compiler can’t know which functions might be above you on the call stack
when you throw an exception.
Therefore, there’s a prett y strong argument for turning off structured ex-
ception handling in your game engine altogether. This is the approach em-
ployed at Naughty Dog and also on most of the projects I’ve worked on at
Electronic Arts and Midway. Console game engines should probably never
use SEH, because of a console’s limited memory and processing bandwidth.
However, a game engine that is intended to be run on a personal computer
might be able to use SEH without any problems.
There are many interesting articles on this topic on the web. Here are links
to a few of them:
z htt p://www.joelonsoft ware.com/items/2003/10/13.html
z htt p://www.nedbatchelder.com/text/exceptions-vs-status.html
z htt p://www.joelonsoft ware.com/items/2003/10/15.html

3.3.3.3. Assertions
An assertion is a line of code that checks an expression. If the expression evalu-
ates to true, nothing happens. But if the expression evaluates to false, the pro-
gram is stopped, a message is printed, and the debugger is invoked if possible.
Steve Maguire provides an in-depth discussion of assertions in his must-read
book, Writing Solid Code [30].
Free download pdf