57

I have a function foo that can throw a bar exception.

In another function I call foo but I have the ability to add some more detail to the bar exception if thrown. (I'd rather not pass such information as a parameter to foo as it doesn't really belong there due to the generic nature of that function.)

So I do this in the caller:

try {
    foo();
} catch (bar& ex){
    ex.addSomeMoreInformation(...);
    throw;
}

Will throw re-throw the modified exception or do I need to use throw ex;? The latter would presumably take a value copy so I'd rather not do that. Would throw take a value copy too? I suspect it wouldn't.

(I'm aware I could verify but I'm concerned about stumbling on an unspecified or undefined construct so would like to know for sure).

0

5 Answers 5

61

Actually, the standard is very precise here. [except.handle]/17:

When the handler declares a reference to a non-constant object, any changes to the referenced object are changes to the temporary object initialized when the throw-expression was executed and will have effect should that object be rethrown.

And [except.throw]/8:

A throw-expression with no operand rethrows the currently handled exception (15.3).

0
26

C++11 §15.1/8:

A throw-expression with no operand rethrows the currently handled exception (15.3). The exception is reactivated with the existing temporary; no new temporary exception object is created.

4
  • 16
    Please add a line of explanation in non-standardese. This way I don't even understand the answer. Does the modification take effect or not?
    – marczellm
    Commented Nov 13, 2014 at 11:19
  • 3
    @marczellm "no new temporary exception object is created"
    – Theolodis
    Commented Nov 13, 2014 at 14:55
  • @marczellm: "no new temporary exception object is created" means that a new object isn't created. Instead the current one is reused. Catching by reference means one have a reference directly to that object, and it doesn't lose the modifications done to it. Please note that the statement about no new temporary would be meaningless if that didn't have observable effect. So this is clue to understanding standardese: ask, for each interpretation, "is it meaningful?". Most every statement in the standard is meaningful. Commented Nov 19, 2014 at 17:11
  • @marczellm: All that said, to succeed with C++ programming it is necessary to all the time think forward at least one single step. An assertion by someone at SO about what something in the standard or some documentation, means, is generally worthless, because about half the SO answers are technically incorrect (yes, even those written by high rep users). Unless you can verify it via your own thinking it's no good. And when that thinking is just a single step of inference, as here, then much better to think oneself than to be spoon-fed a putative explanation and then have more to verify. Commented Nov 19, 2014 at 17:17
3

In this case you should use throw to get the desired behavior...i.e throw would throw the modified exception as the exception was caught by reference.

Let me try to make difference between these throw explicit through examples:-

class exception
{
};

class MyException : public exception
{
};

void func()
{
  try
  {
    throw MyException();
  }
  catch( exception& e )
  {
    //do some modification.
    throw;                    //Statement_1
    throw e;                  //Statement_2
   }
}

Statment_1:-

What throw does is it just re-throws what the current exception is i.e it doesn't make further copies ( as was made when exception was thrown initially). So if you make any changes to the caught exception here...it would also be there in the caller routine.

Statement_2:-

This is throwing the "exception" which was originally caught as MyException i.e it would make the copy again. So, just forget about changes you made it won't even passes or*ginal exception to the caller. It throws "exception" to the caller routine.

Hope I am clear ( and RIGHT ON TRACK OF C++ STANDARD )enough...

2
  • 1
    I don't see how throw e is going to magically "forget about changes you made". The copy may not be desirable but you're still copying the exception object after you modified it. Commented Nov 12, 2014 at 17:18
  • 1
    Oh, true, that's a problem. Okay. HOWEVER the fact that slicing will be performed is not the same as saying that the OP's modifications will be lost: they won't. Commented Nov 12, 2014 at 17:36
3

throw (without an exception object) will rethrow the current exception. (must be inside catch block, otherwise std::terminate is called). Because you changed the reference of current exception object, you don't need to explicilty throw the object and throw re-throw the modified exception and no new temporary object is created.

6
  • 3
    Right, he knows that. You did not answer the question. Commented Nov 12, 2014 at 17:17
  • 2
    @LightnessRacesinOrbit you don't need to explicilty throw the object is the answer I believe. Before that is the reason why. I guess that was something you didn't like and downvoted. Commented Nov 13, 2014 at 8:38
  • No mate, that is not the answer. The OP wants information on what throw does when the exception object has been modified. Your answer doesn't even come close to explaining that. It just says "it will rethrow the current exception" — yes, the OP knows that already. He's trying to get more specifics about how that works. No need to get so defensive. Commented Nov 13, 2014 at 10:27
  • 2
    @LightnessRacesinOrbit: Ad rem: The fact that the OP asks whether he needs to throw ex in order to throw the modified object implies that he is not sure whether the modified exception is the current exception or whether something like a hidden copy on write has taken place. The OP answers that question. Ad "defensive": An understandable reaction at a comment perceived debasing. The brisk statement "did not answer the question" without elaborating is reputation harming in a community which bases reputation, literally, on subject knowledge and competence. Commented Nov 13, 2014 at 11:24
  • 2
    @LightnessRacesinOrbit I didn't scold. I tried to show a plausible chain of thoughts indicating that the OP may have been insecure exactly about the statement Gyapti made. The question as I understand it actually doesn't make sense if the OP is sure that ex refers to the original exception, so that any changes are incorporated in the exception re-thrown by a bare throw, as opposed to a throw ex. The OP's asking this question thus imo demonstrates exactly the opposite of what you think it says. It is good to be more elaborate in order to find out what exactly is said and meant. Commented Nov 13, 2014 at 18:22
2

according to this,throwing exceptions in c++ can be done in two ways:

  1. throw expression: First, copy-initializes the exception object from expression (this may call the move constructor for rvalue expression, and the copy/move may be subject to copy elision), then transfers control to the exception handler with the matching type whose compound statement or member initializer list was most recently entered and not exited by this thread of execution.
  2. throw: Rethrows the currently handled exception. Abandons the execution of the current catch block and passes control to the next matching exception handler (but not to another catch clause after the same try block: its compound-statement is considered to have been 'exited'), reusing the existing exception object: no new objects are made. This form is only allowed when an exception is presently being handled (it calls std::terminate if used otherwise). The catch clause associated with a function-try-block must exit via rethrowing if used on a constructor.

So to underline my answer, throw should be fine in your case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.