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?
- 3Are you asking about thelifetime?Joren– Joren2009-10-31 11:45:23 +00:00CommentedOct 31, 2009 at 11:45
- Yes..when is it going to be destructed?Naveen– Naveen2009-10-31 11:46:45 +00:00CommentedOct 31, 2009 at 11:46
- 10To clarify Joren's question: the termscope usually refers to the region (lines of code) where the variable has a name. The wordscope is often misused to meanlifetime, which is, as you understood, how long the variable actually resides in memory.Thomas– Thomas2009-10-31 12:06:55 +00:00CommentedOct 31, 2009 at 12:06
- 2Thanks 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– Kit102016-07-13 17:42:35 +00:00CommentedJul 13, 2016 at 17:42
3 Answers3
When athrow 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 anyconst andvolatile qualifiers. For class types this means thatcopy-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 acatch block, the name initialized with the caught exception object is initialized with this exception object and not the argument tothrow, even if this was an lvalue.
If youcatch 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.
1 Comment
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 outercatch(...) the last block?The exception object is available only incatch block. You cannot use the exception object outside thecatch block. Following steps happen when you throw an exception and catch:
try{ MyException anObject; throw anObject; //1}catch(MyException exObject){}- The
throwclause (//1) receives the local objectanObject, and treats it as a value argument: it creates a copy of theanObject. - the
catchhandler catches an MyException Object,which again is a value parameter. At this moment another copy is created. - If the
catchhandler would have implemented so as to receive a reference to an object(catch (MyException &o)), the second copy is avoided. - if
catchhandler receives the exception object byconst&then you can only callconstmethods.
1 Comment
throw statement is a pretty bad idea, precisely because that could lead to a redundant copy. Dothrow Myexception(); instead. Apart from that, if you declare an exception object with a name (as in this answer) thenof course it can be accessed outside thecatch block in the scope it was declared in — in this case, inside thetry block.First of all, the object you throw goes out of scope almost immediately. What's going to be caught by exception handlers is acopy of original object. That copy will be deleted after catch handler is executedunless 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.
Explore related questions
See similar questions with these tags.