Modifying exceptions inside a catch block
-
Hello, Here is the question: Is it possible to modify an exception inside a catch block and then rethrow it ? Under http://www.codeproject.com/cpp/ANSI-cpp-dec96/except.asp the only point I see connected to this question is: 19When the handler declares a non-constant object, any changes to that object will not affect the temporary object that was initialized by execution of the throw-expression. When the handler declares a refer- ence to a non-constant object, any changes to the referenced object are changes to the temporary object initialized when the throw-expres- sion was executed and will have effect should that object be rethrown. But this is confusing to me because it seems that is possible to modify a const exception while it is not possible to modify a non const exception: which is exactly the contrary that I would expect ! like:
catch ( BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); //not ok throw; // rethrow the same exception } catch ( const BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); // ok throw; // rethrow the same exception }
Thank you in advance for any answer Marcello P.S. If it is not clear enough what I need to do, here is an example:typedef std::basic_string String; class BasicException : public std::exception { public: BasicException( const VCF::String & message ) { message_ = message; } void setMessage( const VCF::String & message ) { message_ = message; } String getMessage () { return message_; } String message_; } class A { void f() { bool error = true; if ( error ) { throw BasicException( "original message" ); } } void g() { try { f(); } catch ( BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); throw; // rethrow the same exception } }; int main(int argc, char *argv[]) { A a; try { a.g(); } catch ( BasicException& e ) { cout << e.getMessage() << endl; } }
-
Hello, Here is the question: Is it possible to modify an exception inside a catch block and then rethrow it ? Under http://www.codeproject.com/cpp/ANSI-cpp-dec96/except.asp the only point I see connected to this question is: 19When the handler declares a non-constant object, any changes to that object will not affect the temporary object that was initialized by execution of the throw-expression. When the handler declares a refer- ence to a non-constant object, any changes to the referenced object are changes to the temporary object initialized when the throw-expres- sion was executed and will have effect should that object be rethrown. But this is confusing to me because it seems that is possible to modify a const exception while it is not possible to modify a non const exception: which is exactly the contrary that I would expect ! like:
catch ( BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); //not ok throw; // rethrow the same exception } catch ( const BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); // ok throw; // rethrow the same exception }
Thank you in advance for any answer Marcello P.S. If it is not clear enough what I need to do, here is an example:typedef std::basic_string String; class BasicException : public std::exception { public: BasicException( const VCF::String & message ) { message_ = message; } void setMessage( const VCF::String & message ) { message_ = message; } String getMessage () { return message_; } String message_; } class A { void f() { bool error = true; if ( error ) { throw BasicException( "original message" ); } } void g() { try { f(); } catch ( BasicException& e ) { e.setMessage( e->getMessage() + " some more informations here " ); throw; // rethrow the same exception } }; int main(int argc, char *argv[]) { A a; try { a.g(); } catch ( BasicException& e ) { cout << e.getMessage() << endl; } }
The paragraph states precisely what one would expect, i.e that non-const references to exception objects allow for modyfing and the changes will go up the stack if the object is rethrown. That is, no surprises here. If you read it carefully, the first part says a non-constant object, as in
catch(BasicException e)
and the second refers to non-constant references as in
catch(BasicException**&** e)
In the first case, what the
catch
handler gets is a copy of the thrown object, exactly as when you call a regular function with signaturefoo(BasicExceotion e)
. In the second case, you get a reference to the original object and modifying can be done freely. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
The paragraph states precisely what one would expect, i.e that non-const references to exception objects allow for modyfing and the changes will go up the stack if the object is rethrown. That is, no surprises here. If you read it carefully, the first part says a non-constant object, as in
catch(BasicException e)
and the second refers to non-constant references as in
catch(BasicException**&** e)
In the first case, what the
catch
handler gets is a copy of the thrown object, exactly as when you call a regular function with signaturefoo(BasicExceotion e)
. In the second case, you get a reference to the original object and modifying can be done freely. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo