Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 642089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:08:00+00:00 2026-05-13T21:08:00+00:00

Will the following append() in the catch cause the rethrown exception to see the

  • 0

Will the following append() in the catch cause the rethrown exception to see the effect of append() being called?

try {
  mayThrowMyErr();
} catch (myErr &err) {
  err.append("Add to my message here");
  throw; // Does the rethrow exception reflect the call to append()?
}

Similarly, if I rewrite it this way, will bit slicing occur if the actual exception is derived by myErr?

try {
  mayThrowObjectDerivedFromMyErr();
} catch (myErr &err) {
  err.append("Add to my message's base class here");
  throw err; // Do I lose the derived class exception and only get myErr?
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:08:00+00:00Added an answer on May 13, 2026 at 9:08 pm

    In both cases, since you catch by reference, you are effectively altering the state of the original exception object (which you can think of as residing in a magical memory location which will stay valid during the subsequent unwinding — 0x98e7058 in the example below). However,

    1. In the first case, since you rethrow with throw; (which, unlike throw err;, preserves the original exception object, with your modifications, in said “magical location” at 0x98e7058) will reflect the call to append()
    2. In the second case, since you throw something explicitly, a copy of err will be created then thrown anew (at a different “magical location” 0x98e70b0 — because for all the compiler knows err could be an object on the stack about to be unwinded, like e was at 0xbfbce430, not in the “magical location” at 0x98e7058), so you will lose derived-class-specific data during the copy-construction of a base class instance.

    Simple program to illustrate what’s happening:

    #include <stdio.h>
    
    struct MyErr {
      MyErr() {
        printf("  Base default constructor, this=%p\n", this);
      }
      MyErr(const MyErr& other) {
        printf("  Base copy-constructor, this=%p from that=%p\n", this, &other);
      }
      virtual ~MyErr() {
        printf("  Base destructor, this=%p\n", this);
      }
    };
    
    struct MyErrDerived : public MyErr {
      MyErrDerived() {
        printf("  Derived default constructor, this=%p\n", this);
      }
      MyErrDerived(const MyErrDerived& other) {
        printf("  Derived copy-constructor, this=%p from that=%p\n", this, &other);
      }
      virtual ~MyErrDerived() {
        printf("  Derived destructor, this=%p\n", this);
      }
    };
    
    int main() {
      try {
        try {
          MyErrDerived e;
          throw e;
        } catch (MyErr& err) {
          printf("A Inner catch, &err=%p\n", &err);
          throw;
        }
      } catch (MyErr& err) {
        printf("A Outer catch, &err=%p\n", &err);
      }
      printf("---\n");
      try {
        try {
          MyErrDerived e;
          throw e;
        } catch (MyErr& err) {
          printf("B Inner catch, &err=%p\n", &err);
          throw err;
        }
      } catch (MyErr& err) {
        printf("B Outer catch, &err=%p\n", &err);
      }
      return 0;
    }
    

    Result:

      Base default constructor, this=0xbfbce430
      Derived default constructor, this=0xbfbce430
      Base default constructor, this=0x98e7058
      Derived copy-constructor, this=0x98e7058 from that=0xbfbce430
      Derived destructor, this=0xbfbce430
      Base destructor, this=0xbfbce430
    A Inner catch, &err=0x98e7058
    A Outer catch, &err=0x98e7058
      Derived destructor, this=0x98e7058
      Base destructor, this=0x98e7058
    ---
      Base default constructor, this=0xbfbce430
      Derived default constructor, this=0xbfbce430
      Base default constructor, this=0x98e7058
      Derived copy-constructor, this=0x98e7058 from that=0xbfbce430
      Derived destructor, this=0xbfbce430
      Base destructor, this=0xbfbce430
    B Inner catch, &err=0x98e7058
      Base copy-constructor, this=0x98e70b0 from that=0x98e7058
      Derived destructor, this=0x98e7058
      Base destructor, this=0x98e7058
    B Outer catch, &err=0x98e70b0
      Base destructor, this=0x98e70b0
    

    Also see:

    • Scope of exception object in C++
    • Throwing … “by reference”
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following: try { FileStream fileStream = new FileStream(C:\files\file1.txt, FileMode.Append); } catch (DirectoryNotFoundException
Will the following code cause a ConcurrentModificationException or other side effects? ArrayList<String> newList =
Will the following program cause any problem during compiling and execution process? class A{
The following code will: Test to see if the characters in a div are
My following code will only alert once from Firebug, using jQuery's append. I was
how will i decode the string in the following line $(# + result[DivName]).append(result[SavedValue]); ok
I am having following variables in my powershell script $Buildlog=FormalBuild.log $Logger=/flp1:Append;LogFile=$Buildlog;Verbosity=Diag; #Logger will use
Will the following media queries behave exactly same on mobile devices and computers or
Will the following query evaluate to true (1), false (0), or NULL? SELECT '%'
Will the following filter create an arbitrary shape? filter(product.product*store.store, my_measure>1.0) We place it in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.