Click here to Skip to main content
15,890,438 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Are exceptions bad? Pin
Taka Muraoka16-Dec-02 12:52
Taka Muraoka16-Dec-02 12:52 
GeneralRe: Are exceptions bad? Pin
Tim Smith16-Dec-02 13:15
Tim Smith16-Dec-02 13:15 
GeneralRe: Are exceptions bad? Pin
Nemanja Trifunovic16-Dec-02 17:26
Nemanja Trifunovic16-Dec-02 17:26 
AnswerRe: Are exceptions bad? Pin
Tim Smith16-Dec-02 13:20
Tim Smith16-Dec-02 13:20 
AnswerRe: Are exceptions bad? Pin
Matt Gullett16-Dec-02 17:06
Matt Gullett16-Dec-02 17:06 
AnswerRe: Are exceptions bad? Pin
Alvaro Mendez16-Dec-02 17:09
Alvaro Mendez16-Dec-02 17:09 
AnswerRe: Are exceptions bad? Pin
Nemanja Trifunovic16-Dec-02 17:30
Nemanja Trifunovic16-Dec-02 17:30 
AnswerRe: Are exceptions bad? Pin
nde_plume16-Dec-02 17:33
nde_plume16-Dec-02 17:33 
Exeptions, like anything are bad if you misuse them.
Exceptions are designed for major errors that essentially
stop processing at a macro level. To put it in rather
over precise terms, exceptions should be used for the
sorts of errors you are going to report to you users,
(usually acompanied by "Save your data, this system
is in trouble.") Since it is generally a user interaction,
performance per se is not very relevant (becuase people
operate much slower than computers.)

Exceptions are important for writing good quality code
for several reasons:

1. They allow a much cleaner separation of very unlikely
error handling from the main flow of code, meaning
that your code is generally clearer. As a trivial
but important example of that, exceptions mean you
don't need to check the return code of new for NULL
(our of memory.)

2. As a corollory of the above, code is rather eaiser to
write, because you are not constantly distracted by
what to do if your run out of memory, the network goes
down, the database locks where it shouldn't etc.

3. Also as a corollory, error checking is much more
consistently done in code. How many times have you
forgotten to check the return code of malloc? Huh?
You know that that one rare occasion when you run out
of memory will be in the one out of a hundred mallocs
you forgot to check.

4. Exceptions make it feasible to send data with your
error code, something which is either hard or a hack
to do with return codes. By data I mean significant
data, rather than just an integer indication of the
error type. For example, you can throw an exception
indicating which database record you couldn't lock,
or where the network accidentally terminated during
your ftp session.

5. Using the mechanisms for rethrowing exceptions, the
above can be made even more powerful by decorating
the exception as the stack unwinds. (For example,
you catch a network error in your network code, and
rethrow the exception with the IP address attached.
This is caught in your FTP code, which adds information
to the exception that it was an FTP session, and what
file, which is again rethrown, then your web design
tool catches that again, and reports a detailed error
message to the user. Such collection of data from
disparate locations on the stack would be very hard
to do otherwise.)

6. Exceptions allow you to jump way out of context to the
best place to handle the exception (somewhere down
the call stack), but do so in such a way as to largely
prevent leaking of resources, and nautral clean up by
calling destructors on the way down. (Note this doesn't
work perfectly, unless you manage to stay away from
raw new and delete -- which is generally possible with
smart pointers and the STL.)

7. Exceptions are a necessary part of implementing clean
Object oriented designs. Why? Because if you find an
error in your constructor, you cannot return an error
code (because constructors don't return values.) You
need to throw an exception. The only other approach is
to use an error flag in the object, which leaks your
abstraction, making for more complex programs.

Anyways there are seven reasons off the top of my head.

BTW, one poster suggested that exception handling was
fat because it required unwinding the stack and calling
the destructors. That isn't true. Exception handling is
inherently wasteful of time and memory simply because
it is the necessary trade off so that exception handling
causes zero cost on code that doesn't use them.

AnswerRe: Are exceptions bad? Pin
Christian Graus16-Dec-02 20:49
protectorChristian Graus16-Dec-02 20:49 
GeneralRe: Are exceptions bad? Pin
Joe Woodbury17-Dec-02 12:17
professionalJoe Woodbury17-Dec-02 12:17 
GeneralRe: Are exceptions bad? Pin
Christian Graus17-Dec-02 12:29
protectorChristian Graus17-Dec-02 12:29 
GeneralRe: Are exceptions bad? Pin
Joe Woodbury17-Dec-02 12:46
professionalJoe Woodbury17-Dec-02 12:46 
GeneralRe: Are exceptions bad? Pin
Christian Graus17-Dec-02 12:59
protectorChristian Graus17-Dec-02 12:59 
GeneralRe: Are exceptions bad? Pin
Joe Woodbury17-Dec-02 16:45
professionalJoe Woodbury17-Dec-02 16:45 
GeneralRe: Are exceptions bad? Pin
Christian Graus18-Dec-02 18:00
protectorChristian Graus18-Dec-02 18:00 
GeneralRe: Are exceptions bad? Pin
Joe Woodbury19-Dec-02 5:17
professionalJoe Woodbury19-Dec-02 5:17 
GeneralFlicker free "glass" window Pin
HAB16-Dec-02 11:46
HAB16-Dec-02 11:46 
GeneralRe: Flicker free "glass" window Pin
Shog916-Dec-02 11:59
sitebuilderShog916-Dec-02 11:59 
GeneralRe: Flicker free "glass" window Pin
HAB16-Dec-02 12:17
HAB16-Dec-02 12:17 
GeneralShow/Hide Buttons in Visual C++ Pin
Neal McGough16-Dec-02 11:33
Neal McGough16-Dec-02 11:33 
GeneralRe: Show/Hide Buttons in Visual C++ Pin
Alvaro Mendez16-Dec-02 11:47
Alvaro Mendez16-Dec-02 11:47 
GeneralRe: Show/Hide Buttons in Visual C++ Pin
Neal McGough16-Dec-02 15:01
Neal McGough16-Dec-02 15:01 
QuestionCVS questions: what files to commit? Pin
Code4Food16-Dec-02 10:43
Code4Food16-Dec-02 10:43 
AnswerRe: CVS questions: what files to commit? Pin
Jörgen Sigvardsson16-Dec-02 11:30
Jörgen Sigvardsson16-Dec-02 11:30 
GeneralRe: CVS questions: what files to commit? Pin
Code4Food16-Dec-02 12:45
Code4Food16-Dec-02 12:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.