Re: Exception handling
Seungbeom Kim wrote:
mattb wrote:
Reading the recent post 'How do you exception in your daily C++
programming?' brought home to me, once again, that I have no idea how
to apply exceptions and exception handling in my software. I am
approaching c++ from C background and find the notion of exception
handling exceptionally hard to get my head around (at least the idea
of applying it in a consistent and useful manner throughout my
software). I am reading what I believe to be good quality, regarded
text books in order to learn c++, and using this group as a backup
resuource also, but find that most descriptions of exceptions and
exception handling go no further than explaining the basics of the
mechanism itself whilst providing little advice on how to apply as a
technique. Can anyone offer any advice on where to find such
information? I am also a member of the ACCU and have recently taken
to reading DDJ when I can find the time without my wife shouting at
me.
I have heard a lot about:
- comparison between return codes and exceptions
- making the code exception-safe (with RAII, etc)
but not as much about how to actually /use/ exceptions:
e.g. how to design exception classes, and where to install
catch blocks (exception handlers) and what to do in them.
If it is just me that felt this way, can anyone recommend some good
material about these topics? Otherwise, isn't it about time we also
paid attention to these bigger pictures than all those RAII stuff?
Hmm yes. If there are any answers to that, they have eluded me as well :-)
Note also what Goran wrote in his other reply:
[q]
P.S. One can also discuss role of exceptions in error _reporting_
(error info propagation, if you will). This is IMO quite orthogonal
from use of exception for error handling, but an important feature of
exceptions.
[/q]
Personally I think having std::exception as a base class is all good and
well, but from there on, the usability especially for error reporting
very quickly goes the roll-your-own way.
I had a quick look at boost::exception and I also use a custom chained
exception class in a subproject of mine, but I'm still not satisfied
with my error reporting.
If one want's to check though where *not* to install exception handlers,
and what *not* to do in them, I recommend a look at MFCs
CDialog::DoModal() (located in dlgcore.cpp for those interested).
The handler there basically looks like so:
catch(CException* e)
{
DELETE_EXCEPTION(e);
m_nModalResult = -1;
}
Great isn't it?
It certainly reminds me of the "if(error) return FALSE;" approach where
subsequently the return code will be ignored of course.
br,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]