これは、単純な形式のスタック トレースを実装するカスタム例外クラスを作成する最初の試みです。
これは.hです:
class error {
public:
error ();
error (const error & err);
~error ();
int level ();
private:
int _level;
};
そして、これは .cpp です:
error::error ()
: _level (0) {}
error::error (const error & err)
: _level (err._level) {}
error::~error () {}
int error::level () {
return ++_level;
}
次に、2 つのマクロを定義します。1 つはエラーを作成して最初にスローするマクロ (INIT_ERROR) で、もう 1 つはキャッチされたエラーをキックするマクロ (KICK_ERROR) です。
#define WHERE(stream) {fprintf (stream, " %-30s [%s: %3d]\n", __FUNCTION__, __FILE__, __LINE__);}
#define INIT_ERROR {fprintf (stderr, "# 0"); WHERE (stderr); throw error ();}
#define KICK_ERROR {fprintf (stderr, "# %2d", err.level ()); WHERE (stderr); throw;}
ご想像のとおり、使用方法は次のとおりです。
if (something wrong)
INIT_ERROR;
初めて、そして:
try {
// some code
}
catch (error & err) {
KICK_ERROR;
}
他のすべての時間。
ただし、DrMemory (私は Windows XP で作業しています) は、まだ到達可能なブロックについて警告します:
ERRORS FOUND:
0 unique, 0 total unaddressable access(es)
0 unique, 0 total uninitialized access(es)
0 unique, 0 total invalid heap argument(s)
0 unique, 0 total GDI usage error(s)
0 unique, 0 total warning(s)
0 unique, 0 total, 0 byte(s) of leak(s)
0 unique, 0 total, 0 byte(s) of possible leak(s)
3 unique, 3 total, 16 byte(s) of still-reachable allocation(s)
ERRORS IGNORED:
3 potential leak(s) (suspected false positives)
完全を期すために、これがメインです:
void fun3 () {
fprintf (stderr, "nothing special");
INIT_ERROR;
}
void fun2 () {
try {
fun3 ();
}
catch (error & err) {
KICK_ERROR;
}
}
void fun1 () {
try {
fun2 ();
}
catch (error & err) {
KICK_ERROR;
}
}
int main () {
try {
fun1 ();
}
catch (error & err) {
cerr << "error catched in main" << endl;
}
}
これは私のコードで何か問題がありますか? 提案?