记录重新抛出错误的函数

Ruu*_*oef 5 c++ doxygen c++11

我想知道在 doxygen 中记录以下内容的正确方法是什么。

有一个定义一些验证器的类,例如:

class Validators {
    /**
    * @fn A
    * @brief sees if x is too large.
    * @param[in] x the input to validate
    * @throws runtime_error when otx is too large.
    */
    static void A(int x) {
        if (x > 5) {
            throw std::runtime_error("x too large");
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

在如下函数中使用此验证器:

#include "validator.h"

class MyClass {
public:
    void setX(int x) {
        Validators::A(x);
    }
};
Run Code Online (Sandbox Code Playgroud)

我应该如何记录setX()重新抛出由 引发的runtime_error A(),或者我根本不应该记录它?

Ruu*_*oef 0

为了巧妙地做到这一点,我必须稍微更改一下我的代码:

#include "validator.h"
class MyClass {
public:
    void setX(int x) {
        try {
            Validators::A(x);
        }
        catch (std::runtime_error & e) {
            throw e
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

这样做再次添加@throws到 Doxygen 是有意义的,现在它显然被重新抛出了。

  • 只需使用“ throw;”而不是“ throw e;”来避免可能的切片。 (2认同)