4

因此,我尝试基于 Java Exception 类为 C++ 编写一个简单的基 Exception 类。我确信那里已经有很棒的库,但我这样做是为了练习,而不是生产代码,我很好奇并且一直想学习。Java 的异常所做的其中一件事,我也想实现,是“原因”的概念。在 Java 中,带有原因的新异常如下所示:

Exception cause = new Exception();
Exception newExcept = new Exception(cause);

但是,在 C++ 中,将异常作为参数传递给构造函数是调用复制构造函数的方式。因此,复制异常和创建一个有原因的新异常之间存在概念上的脱节。显然,这在 Java 中不是问题。

我想我只是想知道处理这个问题的最佳方法是什么。我的一些想法是:

  • 用虚拟变量进行微分
  • 只需创建新异常,并调用 setCause() 方法
  • 像复制构造函数 isException(Exception &)和带有原因的构造函数是Exception(Exception *)

谢谢

4

4 回答 4

5

异常 - 当在堆栈上分配时(我强烈推荐这个) - 在 catch 子句之后被释放。因此,您需要在新创建的异常中创建“内部”异常的副本。如果您捕获异常的基类,除非您为异常提供克隆方法,否则它将失去正确的类型。

#include <string>
#include <exception>

class MyBaseException : public std::exception
{
public:
    MyBaseException(const std::string& what = std::string("MyBaseException"))
        : m_BaseException(0), m_What(what) {}  //Constructor without inner exception

    MyBaseException(const MyBaseException& innerException, const std::string& what = std::string("MyBaseException"))
        : m_BaseException(innerException.clone()), m_What(what) {}  //Constructor with inner exception

    template <class T>  // valid for all subclasses of std::exception
    MyBaseException(const T& innerException, const std::string& what = std::string("MyBaseException"))
        : m_BaseException(new T(innerException)), m_What(what) {}

    virtual ~MyBaseException() throw()
        { if(m_BaseException) { delete m_BaseException; } } //don't forget to free the copy of the inner exception
    const std::exception* base_exception() { return m_BaseException; }
    virtual const char* what() const throw()
        { return m_What.c_str(); } //add formated output for your inner exception here
private:
    const std::exception* m_BaseException;
    const std::string m_What;
    virtual const std::exception* clone() const
        { return new MyBaseException(); } // do what ever is necesary to copy yourselve
};

int main(int argc, char *argv[])
{
    try {
        try {
            throw std::exception();
        }
        catch(const std::exception& e) {
            throw MyBaseException(e, "bad");
        }
    }
    catch (const MyBaseException& e) {
        throw MyBaseException(e, "even worse");
    }
    //throw MyBaseException(1, "will not compile");
}
于 2010-09-10T06:34:01.653 回答
2

您可以使用工厂模型:

Exception cause = Exception.Create();
Exception newExcept = Exception.Create( Exception cause );
于 2010-09-10T04:47:12.493 回答
1

只需将原因异常的字符串添加到当前异常中即可:

try
{
    throw std::runtime_error("Failed to work");
}
catch(std::exception const& e)
{
    // New exception (add origianl exception text).
    throw std::runtime_error(std::string("We were doing poobar when: ") + e.what());
}
于 2010-09-10T04:56:15.513 回答
0

您的问题不清楚,甚至似乎不是正确的 Java,而是库支持的 Java 习语。我猜你描述的成语是将原始异常作为参数传递给你在重新抛出时创建的异常。

解决方案是创建一个 library_exception (或任何您想调用的名称)

class library_exception: public std::exception
{
 ...
 public:
   library_exception(const  std::exception &e)
 ...
}
...
catch(const std::exception &e)
{
  ...
  throw library_exception(e);
}

不同的类没有调用拷贝构造函数。

于 2010-09-10T05:47:42.317 回答