Re: Are you kidding me?
Posted: Fri May 25, 2012 8:08 am
So my assumption is incorrect? Its the Try...catch that actually causes that behavior instead of the actual error itself?
The Next Generation of 2D Roleplaying Games
http://elysianshadows.com/phpBB3/
Every language has its own idea of exactly how exceptions should work, so C++ doesn't require you to use them. In C, which never had exceptions anyway, developers often indicate error states by returning some error indicator (eg. NULL) if there's an error. Basic C++ behaves similarly.mattheweston wrote:So my assumption is incorrect? Its the Try...catch that actually causes that behavior instead of the actual error itself?
Code: Select all
// C
int* hurr = malloc(sizeof(int));
if(!hurr) { // if hurr == NULL, something went screwy
dieInAFire();
}
Code: Select all
// C++
int* hurr = new int;
if(!hurr) { // same basic behavior as C
dieInAFire();
}
Absolutely not. Since conditionals are really the driving force behind any program, CPUs are very good at executing them. Exceptions, on the other hand, are very expensive. The program must stop, then incrementally unravel the the call stack until it reaches some level where it can be caught. Let's say this is your call stack:mattheweston wrote:I can see where this would be beneficial if you had a 1 to 1 ratio of if..else's replacing try...catch blocks, but what if you had several if...elses that would be in a block of code where one try...catch could take care of it? Wouldn't many if..else's be more expensive than one try...catch?
Code: Select all
Vector::divide(float)
Player::interact()
Terrain::handleCollision()
Collidable::resolve(Collidable) <- this catches exceptions of type CollisionException
CollisionManager::resolveCollisions()
World::step()
Game::run() <- this catches all exceptions
main()