Re: Getting the exception object within catch(...)

From: Alf P. Steinbach (
Date: 12/04/03

Date: Thu, 04 Dec 2003 04:29:01 GMT

On Wed, 3 Dec 2003 22:26:21 +0100, "Jacek Dziedzic" <> wrote:

> Hi!
> In my main() function I have a last-resort exception construct that
>like this:
>int main() {
> try {
> // ... program code
> }
> catch(...) { // catch every exception
> std::cout << "An exception occured!" << std::endl;
> };
> It works fine, but I never know *which* exception has occured.
>I seem to remember you can catch an exception object rather than
>an exception class, and then, having the precise object you could
>get the name that's stored somewhere inside it. But the "..." seems
>to get in the way for me. Is there a way to catch a precise exception
>object with catch(...)? Or a workaround for that?

Cy Edmunds and others have already answered the real question.

Now here's an answer to the literal question.

In many cases (actually, most cases of real software) you will have to
deal with non-standard exceptions of various kinds from a number of libraries.
Using try-catch-catch-catch everywhere is then redundant and a maintainance
nightmare. So instead you'd like to use catch(...) and _translate_ that
exception, whatever it may be, to a common form.

In standard C++ you can do that by rethrowing the exception and recatching
the exception within the catch(...), where this rethrowing and recatching is
performed by a common exception translation function (which must know about
all the most common kinds of exceptions in the application).

This technique does not work with some older compilers. In particular, a
compiler bug in Visual C++ prevented using this technique with earlier
incantations of that compiler.