Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Modifying exceptions inside a catch block

Modifying exceptions inside a catch block

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comhelptutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marcello
    wrote on last edited by
    #1

    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; } }

    J 1 Reply Last reply
    0
    • M Marcello

      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; } }

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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 signature foo(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

      M 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        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 signature foo(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

        M Offline
        M Offline
        Marcello
        wrote on last edited by
        #3

        Thank you very much I guess my neurons started crossing with each other Sometimes the C++ specifications language is very hard to follow Now this is very clear :-) Cheers, Marcello

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups