C Exceptions 关于重新抛出原始异常的问题

新手上路,请多包涵

catch中后面的append()会不会导致rethrow的异常看到append()被调用的效果?

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

同样,如果我这样重写,如果实际异常是由 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?
}

原文由 WilliamKF 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 740
2 个回答

在这两种情况下,由于您通过引用捕获,因此您实际上是在更改原始异常对象的状态(您可以将其视为驻留在 一个神奇的内存位置,该位置将在随后的展开期间保持有效 0x98e7058 在下面的示例中)。然而,

  1. 在第一种情况下,由于您使用 throw; throw err; ,通过您的修改,保留原始异常对象,在 0x98e7058 的“神奇位置”中 --- ) 反映对 append() 的调用
  2. 在第二种情况下,由于您明确抛出一些东西,因此将创建 err副本,然后重新抛出(在不同的“神奇位置” 0x98e70b0 因为所有编译器都知道 err could be an object on the stack about to be unwinded, like e was at 0xbfbce430 , not in the “magical location” at 0x98e7058 ),因此在基类实例的复制构造过程中, 您将丢失派生类特定的数据

简单的程序来说明正在发生的事情:

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

结果:

   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

另见:

原文由 vladr 发布,翻译遵循 CC BY-SA 3.0 许可协议

对于第一个问题,是的。

但对于第二个,请参阅弗拉德的回答。您将需要仔细设计您的异常对象来处理复制 ctor。按照惯例,基类无法识别其子类,因此您很可能会丢失派生类携带的额外数据。

原文由 YeenFei 发布,翻译遵循 CC BY-SA 2.5 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题