views:

185

answers:

3

Here's the setup.

I have a C++ program which calls several functions, all of which potentially throw the same exception set, and I want the same behaviour for the exceptions in each function (e.g. print error message & reset all the data to the default for exceptionA; simply print for exceptionB; shut-down cleanly for all other exceptions).

It seems like I should be able to set the catch behaviour to call a private function which simply rethrows the error, and performs the catches, like so:

void aFunction()
{
    try{.../* do some stuff that might throw */...}
    catch(...){handle();}
}

void bFunction()
{
    try{.../* do some stuff that might throw */...}
    catch(...){handle();}
}

void handle()
{
    try{throw;}
    catch(anException)
    {
        ...
        // common code for both aFunction and bFunction
        // involving the exception they threw
        ...
    }
    catch(anotherException)
    {
        ...
        // common code for both aFunction and bFunction
        // involving the exception they threw
        ...
    }
    catch(...)
    {
        ...
        // common code for both aFunction and bFunction
        // involving the exception they threw
        ...
    }
}

Now, what happens if "handle" is called outside of the exception class. I'm aware that this should never happen, but I'm wondering if the behaviour is undefined by the C++ standard.

+1  A: 

If you use throw inside of a catch block, it will rethrow the exception. If you use throw outside of a catch block, it will terminate the application.

Brian R. Bondy
Good answer, although John's was clearer. Thanks!
deworde
+6  A: 

If handle() is called outside the context of an exception, you will throw without an exception being handled. In this case, the standard (see section 15.5.1) specifies that

If no exception is presently being handled, executing a throw-expression with no operand calls terminate().

so your application will terminate. That's probably not what you want here.

John Feminella
Well, there's no sane reason to call "handle()" if you're not inside a catch block, but I was interested in whether what happened would be defined.That was a perfect answer. Thanks!
deworde
+1  A: 

Never, never, never use catch(...) as you might catch application errors that you don't want to catch, e.g. bugs, access violations (depending on how you compiled).

Read the great John Robbins book (Debugging Windows Applications) in which he explains more in detail why you shouldn't do it.

Patrick
http://stackoverflow.com/questions/2183113/using-catch-ellipsis-for-post-mortem-analysis
SF.
This is completely irrelevant to my question.Also, it's inaccurate, as SF's shown above. There are plenty of perfectly good reasons to use (...), as long as you handle it correctly.For example, suppose I want to release a lock on something that's shared. I could wait till it reaches a destructor, but better to do it as soon as I'm sure I no longer need it.I could then rethrow from inside the catch(...){} block if I wanted to ensure that the access violations and bugs still led to termination.
deworde