gpt4 book ai didi

c++ - 自定义错误函数 C++

转载 作者:行者123 更新时间:2023-11-30 01:49:52 26 4
gpt4 key购买 nike

有没有办法获取正在执行代码的行?

例如:

int death() {
cout << "An error has occurred on line " << line << endl;
exit(128);
}

然后,如果我在第 64 行调用 death() 函数,它会告诉我错误发生在第 64 行。

编辑:大多数人似乎都在告诉我使用 __LINE__ 宏。我知道你可以做到:

#define DEATH(message) death(__LINE__, message)

在哪里

int death(int line, string message) {
cout << "An error has occurred at line << line << endl;
}

但我正在考虑以这种方式使用它:

string readFile(string file) {
string str;
ifstream stream(file);
if (!stream) { death(); } //if this is line 23, it prints line 23
stream >> str;
stream.close();
return str;
}

//instead of printing line 64 if readFile() is executed on line 64

并且单独使用 __LINE__ 宏只会返回执行 death() 函数的行。换句话说,如果我在第 64 行执行 readFile(),我希望 death() 打印第 64 行,而不是在 中定义它的行>readFile() 函数。

最佳答案

尝试使用 __LINE__宏观:

The standard predefined macros are specified by the relevant language standards, so they are available with all compilers that implement those standards. Older compilers may not provide all of them. Their names all start with double underscores.

__FILE__

This macro expands to the name of the current input file, in the form of a C string constant. This is the path by which the preprocessor opened the file, not the short name specified in #include or as the input file name argument. For example, /usr/local/include/myheader.h is a possible expansion of this macro.

__LINE__

This macro expands to the current input line number, in the form of a decimal integer constant. While we call it a predefined macro, it's a pretty strange macro, since its "definition" changes with each new line of source code.

__FILE__ and __LINE__ are useful in generating an error message to report an inconsistency detected by the program; the message can state the source line at which the inconsistency was detected.

如果您想获取当前 行以外的行号,则需要在较早的时间点存储来自__LINE__ 的行号。例如,此代码获取调用 death() 的函数的 first 行号:

#include <stdio.h>
#include <stdlib.h>

/* Set up some stuff to keep track of where you died. */
int DEATH_LINE = 0;
#define CAN_DIE DEATH_LINE = __LINE__;
void death() { printf("OH NO, YOU DIED AT LINE %d!\n", DEATH_LINE); }

/* This function can die, so get the first line number using CAN_DIE */
int someFunction() { CAN_DIE
printf("Hello, World!\n");
death();
printf("Goodbye, Cruel World!\n");
return 0;
}

int main() { return someFunction(); }

这会产生以下输出:

Hello, World!
OH NO, YOU DIED AT LINE 10!
Goodbye, Cruel World!

Process returned 0 (0x0) execution time : 0.009 s
Press any key to continue.

或者,您可以获得调用函数的行号,如下所示:

#include <stdio.h>
#include <stdlib.h>

int DEATH_LINE = 0;
#define CAN_DIE(x) ((DEATH_LINE = __LINE__), (x));
void death() { printf("OH NO, YOU DIED AT LINE %d!\n", DEATH_LINE); }

int someFunction() {
printf("Hello, World!\n");
death();
printf("Goodbye, Cruel World!\n");
return 123;
}

int main() { return CAN_DIE(someFunction()); }

这会产生以下输出:

Hello, World!
OH NO, YOU DIED AT LINE 15!
Goodbye, Cruel World!

Process returned 123 (0x7B) execution time : 0.008 s
Press any key to continue.

请注意,第二个示例要求调用函数将另一个函数(即实际调用 death() 的函数)包装在宏中以正确设置当前行号。

关于c++ - 自定义错误函数 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28339474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com