exceptional exceptions in C++

For  a long long time I have read about exceptions, programmed them and also seen how horribly they go wrong. I know and understand that exceptions are not supposed to be an after thought but supposed to be designed along side the classes. Nice and good. However I hate the fact that an exception raised in line 10 gets caught all the way down 100 lines. That sounds eerily similar to the goto statement. It just means that now I have to touch two sections everytime I allocate resources. That’s not my style of writing. It is also one of the many reasons why I use templates. Having written code in a bunch of programming languages, I’ve come to conclude that the lesser the code that we write the fewer the bugs we encounter. The fewer the number of things that a programmer should modify to achieve a goal the fewer the number of  memory leaks and resource leaks. After all how many people have written buggy “hello world” programmes?

Exceptions seem to demand that I write the same statement twice (especially with memory). One possible way to fix this is the shared pointer, auto ptr and a bunch of other constructs that turn pointers into stack elements. Then there is the issue with exceptions sucking up a lot of processor time. The last time I check (won’t mention the platform or the compiler – NDA) I noticed a 15% drop in frame rates. I haven’t used it since and I am unaware of how much better (if at all) the compilers have become.

Then there is the issue with the depth of the exceptions. Everytime I add a new node to the exceptions hierarchy, I seem to miss the fact that the parent will catch the exception if it is listed before the child in the catch block.

Hence I generally go back to returning error codes, especially when I am writing games. Hey, it is afterall just an enum that I declare there, right?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s