45

What is the scope of the exception object in C++? does it go out of scope as soon as catch handler is executed? Also, if I create an unnamed exception object and throw it, then while catching that exception does it matter if I catch it by const reference or a non-const reference?

Naveen
  • 74,600
  • 47
  • 176
  • 233
  • 3
    Are you asking about the *lifetime*? – Joren Oct 31 '09 at 11:45
  • Yes..when is it going to be destructed? – Naveen Oct 31 '09 at 11:46
  • 10
    To clarify Joren's question: the term _scope_ usually refers to the region (lines of code) where the variable has a name. The word _scope_ is often misused to mean _lifetime_, which is, as you understood, how long the variable actually resides in memory. – Thomas Oct 31 '09 at 12:06
  • 2
    Thanks Thomas, this is also consistent with the standard (n4296) 3.8 "lifetime of an object is a runtime property", 3.3 "each particular name is valid only within some possibly discontiguous portion of program text called its scope" - thus, scope refers to source code. I had never thought about this distinction. – Kit10 Jul 13 '16 at 17:42

3 Answers3

44

When a throw expression is evaluated, an exception object is initialized from the value of the expression. The exception object which is thrown gets its type from the static type of the throw expression ignoring any const and volatile qualifiers. For class types this means that copy-initialization is performed.

The exception object's scope is outside of the scope of the block where the throw occurs. Think of it as living in a special exception area off to one side of the normal call stack where local objects live.

Inside a catch block, the name initialized with the caught exception object is initialized with this exception object and not the argument to throw, even if this was an lvalue.

If you catch via non-const reference, then you can mutate the exception object, but not what it was initialized from. You can alter the behaviour of the program if you re-throw the exception in ways that you couldn't if you caught by value or const reference (const_casts aside).

The exception object is destroyed when the last catch block that does not exit via a re-throw (i.e. a parameterless throw expression evaluation) completes.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • What is _the last catch block_? `catch(...) { std::exception *p = nullptr; try { throw; } catch (const std::exception &e) { p = &e; } catch(...) {} if (p) { std::cerr << p->what() << std::endl; } }` Is this valid code? The inner catch doesn't exits via a re-throw. Is the outer _catch(...)_ the last block? – anton_rh Dec 18 '18 at 05:36
9

The exception object is available only in catch block. You cannot use the exception object outside the catch block. Following steps happen when you throw an exception and catch:

try
{
 MyException anObject;
 throw anObject;  //1

}
catch(MyException exObject)
{
}
  • The throw clause (//1) receives the local object anObject, and treats it as a value argument: it creates a copy of the anObject.
  • the catch handler catches an MyException Object,which again is a value parameter. At this moment another copy is created.
  • If the catch handler would have implemented so as to receive a reference to an object (catch (MyException &o)), the second copy is avoided.
  • if catch handler receives the exception object by const& then you can only call const methods.
aJ.
  • 34,624
  • 22
  • 86
  • 128
  • Creating objects outside the `throw` statement is a pretty bad idea, precisely because that could lead to a redundant copy. Do `throw Myexception();` instead. Apart from that, if you declare an exception object with a name (as in this answer) then *of course* it can be accessed outside the `catch` block in the scope it was declared in — in this case, inside the `try` block. – Konrad Rudolph Jul 09 '20 at 13:32
4

First of all, the object you throw goes out of scope almost immediately. What's going to be caught by exception handlers is a copy of original object. That copy will be deleted after catch handler is executed unless you catch it by value (not by reference). In this case there will be another copy created. But you should catch it by reference (preferably const one) anyway.

vava
  • 24,851
  • 11
  • 64
  • 79
  • What about throwing a pointer to something as per MFC? You need to consider objects other than objects of class type as implied in the question and your answer. – Sam Oct 31 '09 at 12:01
  • Pointers get copied too but nobody cares usually :) – vava Oct 31 '09 at 12:10