Are you kidding me?
Moderator: Coders of Rage
-
- Chaos Rift Junior
- Posts: 200
- Joined: Mon Feb 22, 2010 12:32 am
- Current Project: Breakout clone, Unnamed 2D RPG
- Favorite Gaming Platforms: PC, XBOX360
- Programming Language of Choice: C#
- Location: San Antonio,Texas
- Contact:
Re: Are you kidding me?
So my assumption is incorrect? Its the Try...catch that actually causes that behavior instead of the actual error itself?
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Are you kidding me?
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?
So in the case of memory allocation, you can check for an out-of-memory (or any other allocation error) in this way:
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();
}
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
-
- Chaos Rift Junior
- Posts: 200
- Joined: Mon Feb 22, 2010 12:32 am
- Current Project: Breakout clone, Unnamed 2D RPG
- Favorite Gaming Platforms: PC, XBOX360
- Programming Language of Choice: C#
- Location: San Antonio,Texas
- Contact:
Re: Are you kidding me?
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?
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: Are you kidding me?
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()
And even in the simpler case of Vector::divide throwing a CollisionException, the runtime still has to unwind half the stack before it gets handled.
On the other hand, conditionals and returns are both dead-simple and quick to execute. Although it may not be quite as "flexible" to handle errors this way, the added complexity and cost of exceptions make it perfectly reasonable to avoid them.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.