Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

What are the pros and cons of using Exceptions in C++ in relation to game development.

Google style guide says that they don't use Exceptions for a variety of reasons. Are the same reasons pertinent towards game development?

We do not use C++ exceptions... - google-styleguide.googlecode.com

Some issues to think about.

  • How it pertains to the development of a libraries used through multiple projects.
  • How does it affect unit testing, integration testing, etc?
  • What does it do to using third party libraries.
share|improve this question
3  
Oddly no one had any pros –  David Young Aug 18 '10 at 0:21
add comment

6 Answers

up vote 6 down vote accepted

There are also some nasty consequences for using exceptions in C++ if you don't know all the details of how they work. Since many games are written in C++ this may be a factor.

For example, in C++ throwing an exception in a destructor can have serious consequences. It can cause all kinds of undefined behavior and crashes, because you can get trapped in a situation where you have more than one active exception in flight. (See Item #8 of Effective C++ for more info on this.)

share|improve this answer
3  
+1 for Effective C++. Reading the 3rd edition as we speak. Exception are fine with me, as long as you're very strict about where you use them, where you catch them and you can guarantee that any 3rd party code does the same. –  anthony-arnold Aug 15 '10 at 23:29
add comment

As far as I see it there are two main reasons it's not traditionally used in game development:

  • Most game programmers are either graduates, who learned on Java and Haskell, or C programmers, who don't have any experience of exceptions. Hence they're quite afraid of them and don't know how to use them correctly.
  • The stack unwinding when an exception is thrown has performance implications (although this is kind of received wisdom, and some references would be great).
share|improve this answer
2  
Java programmers not knowing how to use Exceptions? Java is based around exception handling. Even the most basic of Java programmers understand the Try, Catch, Finally blocks. You really can't program in Java without either throwing Exceptions or handling them. –  David Young Aug 14 '10 at 22:33
4  
Its more then just stack unwinding. Exceptions add overhead to every function call. codeproject.com/KB/cpp/exceptionhandler.aspx –  Jonathan Fischoff Aug 14 '10 at 23:09
    
@David Young I'm talking more about the strong/weak guarentees, and the more complex side of using exceptions, that university graduates may not be experienced in. To compound that, many universities are much more interested in how you structure your OOP so put less emphasis on learning how to use Java exceptions properly. But you're right I wasn't clear. –  tenpn Aug 15 '10 at 9:55
    
Also, maybe "afraid" is the wrong word. :) –  tenpn Aug 15 '10 at 9:57
2  
I would say they're often the opposite of "afraid" - if you don't have a strict "no exceptions, no exceptions" rule, most recent Java graduates will end up throwing exceptions more often than returning, just like novice C programmers use goto and break rather than write good loop invariants. –  user744 Aug 15 '10 at 15:49
add comment

Exceptions are not supported and therefore highly discouraged in at least one modern console development environment.

Most console developers I know prefer not to use them anyway due to added overhead in the executable and the philosophy that they're just plain not needed. RTTI is viewed the same way.

share|improve this answer
    
Which console is it not supported on? –  David Young Aug 15 '10 at 2:59
    
I'm trying to be coy because of NDAs, even though it's been revealed more specifically on SO before. –  Dan Olson Aug 15 '10 at 4:41
add comment

Of all the projects I worked on in games, none of them used exceptions. Function call overhead is the major reason. As mentioned before, like RTTI, it's, in most studios, not a subject of discussion. Not because most coders come from a Java/academics background, because frankly, most don't.
It doesn't affect unit testing really as you just assert on conditions. For libraries, you're better off without exceptions as you'll force it upon everyone that uses it.
While it makes some situations slightly harder to handle, it also (imo) forces you to have a more controlled setup, as exceptions can lead to abuse. Error tolerance is nice, but not if it leads to sloppy coding.
Mind you, this comes from someone who never used exception handling (well okay, once or twice in tools - it didn't do it for me, I guess it's what you grow up with).

share|improve this answer
1  
You're right, but unfortunately rather than use alternative error-handling, most games programmers just revert to crashing or returning NULL (which will probably lead to a crash anyway). We've not come up with any decent replacement. –  tenpn Aug 15 '10 at 9:58
    
Returning NULL, or an error code, is a perfectly reasonable replacement so long as you check the return codes too. –  JasonD Aug 15 '10 at 11:56
    
Yeah but most games programmers don't. Which is my point. –  tenpn Aug 15 '10 at 13:02
    
Then they wouldn't last very long where I work, code reviews would become quite embarassing. –  Kaj Aug 15 '10 at 16:43
1  
It's a problem of the C++ language as much as anything - if you see a line of code that says function(); you don't instantly know whether an error code is being ignored or not. This is presumably why languages that let you ignore a return value try and coerce you into preferring exceptions. –  Kylotan Aug 17 '10 at 16:35
show 1 more comment

Joel on Software's views of Exceptions.

Interesting view not currently in any of the answers,

The reasoning is that I consider exceptions to be no better than "goto's", considered harmful since the 1960s, in that they create an abrupt jump from one point of code to another. In fact they are significantly worse than goto's:

  1. They are invisible in the source code. Looking at a block of code, including functions which may or may not throw exceptions, there is no way to see which exceptions might be thrown and from where. This means that even careful code inspection doesn't reveal potential bugs.

  2. They create too many possible exit points for a function. To write correct code, you really have to think about every possible code path through your function. Every time you call a function that can raise an exception and don't catch it on the spot, you create opportunities for surprise bugs caused by functions that terminated abruptly, leaving data in an inconsistent state, or other code paths that you didn't think about.

http://www.joelonsoftware.com/items/2003/10/13.html

share|improve this answer
    
Halleluja... Great reference. i've had an aversion with exceptions for exactly this reason, but apparently it is the preferred way of doing things nowadays. Great to see a nice article which gives another view –  Toad Aug 15 '10 at 17:58
1  
+1 because exceptions have a much too high risk to fail late, i.e. when the game's already shipped. –  martinpi Aug 16 '10 at 9:51
    
I completely agree with point number 2. I won't fight against languages that use exceptions throughout, but neither do I consider them the 'right' way to handle error conditions either. –  Kylotan Aug 17 '10 at 16:32
add comment

The most important thing for me as a professional games developer is that exceptions have to be written by people that understand how exceptions work (which there are few in the games industry) debugged by them too, and the underlying code that runs them (which is a branchy mess anyway and will kill your in-order processor) had to be written by people providing your compiler/linker. The problem with that is that they only have a finite amount of resources, so they concentrate on writing better optimisations and fixes for code that games developers do use... which usually isn't exceptions. If you've got an exception bug on modern hardware with new compilers, who are you gonna call? your exception expert who thinks everything is okay with the code, or the vendor who says, yeah, soon, we're just trying to make intrinsics happen more automatically.

There's nothing inherently wrong with exceptions, just they're not good because reality gets in the way of theory.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.