是否可以将异常传递给处理程序,然后重新引发原始异常

Is it possible to pass an exception to a handler and then rethrow the original exception?

本文关键字:异常 然后 程序 原始 新引发 处理 是否      更新时间:2023-10-16

我正在尝试编写代码,当出现错误时,该代码会被传递给处理程序。

class Handler {
   public: virtual void handle(std::exception const& e) = 0;
};
class DoIt {
  Handler* handler;
  public:
  void doStuff() {
    try {
      methodThatMightThrow();
    } catch (std::exception const& e) {
      handler->handle(e);
    }
  }
  void setHandler(Handler* h) { handler = h; }
  void methodThatMightThrow();

}

不同的项目将使用具有不同错误处理技术的此类。

项目1记录错误

class Handler1: public Handler {
  void handle(std::exception const& e) override {
    logError(e.what());
  }
};

项目2传播异常

class Handler2: public Handler {
  void handle(std::exception const& e) override {
    throw e;
  }
};

这两者都应该有效。然而,如果异常是std::exception的子类,Handler2将抛出异常的副本,并丢失任何派生的类信息,这几乎是肯定的

有没有一种好的方法可以重新抛出原始异常,甚至是同一类型的副本?

您可以使用裸throw来重新引发当前异常。

重写Handler2以使用它将给出以下代码:

class Handler2 : public Handler
{
public: void handle(const std::exception& ex) const override
    {
        throw;
    }
};

您不必将异常作为参数发送,并且可以编写更高级的处理程序,这些处理程序可以根据异常的类型执行不同的操作,例如这个简单的处理程序。

class WrapperHandler : public Handler
{
public:
    void handle() const
    {
        try
        {
            throw;
        }
        catch (const notveryserious_exception& ax)
        {
            std::cout << "Not very serious, I'm going to let this slide." << std::endl;
            std::cout << ax.what() << std::endl;
        }
        catch (const myown_exception& ax)
        {
            // Probably serious, will let this propagate up the stack.
            throw;
        }
        catch (...)
        {
            // Bad, bad, bad.. Unhandled exception that we haven't thrown ourselves.
            throw myown_exception("Unhandled exception.");
        }
    }
};

问题是不能抛出引用。Throw需要对象的真实副本。副本正在对抛出点处的类型引用进行切片。

我不知道这个问题有什么解决办法。