Re: Question on goto with try/catch
* Eric J. Holtman:
"Alf P. Steinbach" <alfps@start.no> wrote in
news:he5rst$570$1@news.eternal-september.org:
Don't catch by ellipsis unless you're rethrowing (and possibly picking
up the exception again, but anyway rethrowing) within the catch
handler.
Shouldn't you catch(...) on a module boundary
(like at the main function of a thread), just to
ensure no exceptions cross into a possibly non-C++
area (like the OS).
Or would your answer be "You should know what
exceptions can get to that point, and catch
all of them explicitly"?
It depends. :-)
The advice I gave is generally applicable within C++ code, but a module/language
boundary can be something else.
One technique, made easier with C++0x, is when an exception is caught at the
highest level of a callback invoked from C code (say), to store a clone of the
exception object and translate to an error return value, then rethrow at the
originally calling C++ side of things again.
At the outermost call level of the main function of a thread, if you have any
exception propagating up here then in general the thread has failed. Dealing
properly with that depends on your threading library. A reasonable way, if it is
supported, is to set the thread's exit code to EXIT_FAILURE and in some way make
available a clone of the exception object.
Hm, it seems that in C++98 exception object should always be clonable. :-)
Still, catching by ellipsis is generally ungood without rethrowing, because it
catches all sorts of exceptions, even, with old MSVC, operating system
exceptions like integer division by zero or nullpointer exception -- which
effectively was a language extension, but one worth keeping in mind. Not to
mention, as I believe that I did, pointer exceptions like in MFC, leaking
memory. So, since suppressing catastrophic failures and leaking memory is
ungood, catching by ellipsis without rethrowing is generally ungood.
Cheers & hth.,
- Alf
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]