3 ms·
Throwing an exception is a misnomer. An exception doesn't necessarily mean that an error happened. It means, "please unwind the call stack". Unwinding the ca
by pkaler 16y ago
Throwing an exception is a misnomer. An exception doesn't necessarily mean that an error happened. It means, "please unwind the call stack". Unwinding the call stack in a game is a no-no.
Games, especially the simulation, must be deterministic. An exception would most likely destroy determinism.
The simulation/AI is a big, hairy, ugly set of if/else and switch statements anyway. Adding extra error checking is not an extra burden.
The visualization sends asynchronous commands from the CPU to the GPU. The error condition won't be known right away. Querying the GPU will cause a flush of your command stream and ruin performance.
Dynamic memory allocation is reduced to a minimum during a frame so you don't get out of memory errors. Strings are allocated in a pool. Network sockets, file handles, and other performance intensive resources are not created mid-frame.
Exceptions are not a win for games. It is usually best to run the entire frame and then check for errors all at once at the end.
- dhess 16y ago> Unwinding the call stack in a game is a no-no. For the most part, I agree, which is why I said they're probably only appropriate in games when used judiciously, e.g., for fatal errors.
- palish 16y agoExceptions are actually nice for single methods, even in a game. void Horse::ImOnA() { try { Giddyup(); if (IsDead()) EX_ERROR("Guess I wasn't on it."); if (IsFlippingTheFuckOut()) EX_WARN("Safety hazard! Get the f outta the way!"); Feed(); } catch (CException& ex) { ex.Process("Horse::ImOnA() - ", NO_THROW); } CleanupAfterHorse(); } If the horse is dead, then "Horse::ImOnA() - Guess I wasn't on it." will be printed to the console. If the horse is acting like my mother, then "Horse::ImOnA() - Safety hazard! Get the f outta the way!" will be printed to the console. Etc. So this is a nice way to report errors. It's basically a typed goto. But don't you dare try to use it for flow control! >:( You can report various "levels": EX_DEBUG, EX_WARN, EX_ERROR, EX_FATAL ex.Process() checks what kind of exception it is. And for EX_FATAL, for example, it will terminate the application (after writing the message to a log file). It's quite nice to use exceptions in this way, because you rarely ever need to even think about making your methods "exception safe" at all.