记录重新引发错误的函数

Documenting a function that re-throws an error

本文关键字:函数 错误 新引发 记录      更新时间:2023-10-16

我想知道在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");
}
}
};

在函数中使用此 valdator,例如:

#include "validator.h"
class MyClass {
public:
void setX(int x) {
Validators::A(x);
}
};

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

您应该记录它对调用者意味着什么。 例如

class MyClass {
public:
/**
* @brief sets X
* @param[in] x the new X
* @throws runtime_error when x is invalid.
*/
void setX(int x) {
Validators::A(x);
}
};

为了巧妙地做到这一点,我不得不稍微改变一下我的代码:

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

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