473,770 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
Kindly tell me the costs and benefits of using exceptions?
Aug 13 '07 #1
4 4153
JosAH
11,448 Recognized Expert MVP
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
9,208 Recognized Expert Moderator Expert
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
Strika Amaru
19 New Member
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
9,208 Recognized Expert Moderator Expert
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

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

Similar topics

7
2209
by: Jonathan Fine | last post by:
Giudo has suggested adding optional static typing to Python. (I hope suggested is the correct word.) http://www.artima.com/weblogs/viewpost.jsp?thread=85551 An example of the syntax he proposes is: > def f(this:that=other): > print this This means that f() has a 'this' parameter, of type 'that'. And 'other' is the default value.
125
14850
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
3
5369
by: Will Atkinson | last post by:
Hi All, I'm a relative newbie to SQL Server, so please forgive me if this is a daft question... When I set "Show Execution Plan" on in Query Analyzer, and execute a (fairly complex) sproc, I note that a particular query is reported as having a query cost of "71% relative to the batch" - however, this is nowhere near the slowest executing query in the batch - other queries which take over twice as long are reported as having costs in...
13
5058
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
53
3192
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
34
3631
by: emrahayanoglu | last post by:
Hello Everyone, Now, I'm working on a new web framework. I tried many test on the other programming languages. Then i decided to use python on my web framework project. Now i want to listen all of you. What do you want in that web framework(Easy use of Database, Easy use of XML, GUI Designer, etc...)? I'm wating your answers. Thank you for all answers...!
2
1549
by: estherschindler | last post by:
For a lot of IT people -- everyone from software developers to tech writers to network support folks -- telecommuting is the best personal option. They get a flexible schedule, they aren't bothered by noisy cube-mates, they can code during whichever hours work for them (with the help of IM and email), and so on. Many of us are lucky enough to live this lifestyle; I include myself in this set, as I've been a full time telecommuter for...
89
5768
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
11
263
by: plenty900 | last post by:
I was looking over someone's C++ code today and despite having written perfectly readable C++ code myself, the stuff I was looking at was worse than legalese. The people who are guiding the development of C++ have really made a mess of things, I mean templates and competing libraries and all that just render the code impossible to comprehend. Sure there is going to be a certain amount of complexity, that's a given, but if code is not...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10037
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.