By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 701 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

What are the costs and benefits of using exceptions in C++?

P: 2
Kindly tell me the costs and benefits of using exceptions?
Aug 13 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 10K+
P: 11,448
Kindly tell me the costs and benefits of using exceptions?
Exceptions allow you to organize your *ahem* exception handling much better
and just because exceptions are supposed to be exceptions to the rule they
won't occur much. Use them wereever appropriate; don't use them as an alternative
return mechanism.

Handle exceptions as close to where they were thrown; don't handle exceptions
you can't really handle and never ever muffle exceptions away without a darn
good reason.

On the other hand: throw a most specific exception, i.e. help your caller to find
out what exactly went wrong, i.e. don't just yell "error!" when you can do better.

kind regards,

Jos
Aug 13 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Adding to JosAH:

Plus you can't return errors from your constructors.

You have to use exceptions.

Plus you can't return errors from your destructors.

You have to use exceoptions.

Plus you can't return errors from the iniotialization list. You have to ssue a function try block and so you are back to exceptions.

Plus the C++ Standard Library uses a hierarchy of exception classes. If you use the library you have to use exceptions.

Plus, when new runs out of memory it throws an exception.

Plus, there is automatic rollback of the stack undoing all allocations. I have never seen a C program do this. The code has to be done by hand and no one ever does it. Instead, it usually just does exist() and blows to the command line. OK for 1972 but a disaster on a server.

It's not about cost and benefits. It's about whether you code is going to run or not.
Aug 14 '07 #3

P: 19
Plus you can't return errors from your destructors.

You have to use exceoptions.
Must complain at one gigantic slip here: you should never, never, never, NEVER throw an exception from a destructor. A very good (and pretty funny!) explanation is presented in
http://www.parashift.com/c++-faq-lite/exceptions.html
at section 17.3
Aug 14 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
Originally Posted by weaknessforcats
Plus you can't return errors from your destructors.

You have to use exceoptions.
What I meant was that should you be deleting items in your destructor, or otherwise cleaning up, and encounter an exception, you had better catch it and handle it while still in your destructor. If you don't your program is gonna crash.

I didn't say anything about throwing exceptions from a destructor.
Aug 14 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.