答案是,这取决于您的代码的约束。
collectd打印到标准错误,然后在遇到致命错误时退出。
OpenGL 将设置一些您可以查询的共享状态。忽略此错误通常会导致未定义的行为。
Collectd 有很多线程问题,大多数错误无法由程序修复或恢复。例如,如果一个插件依赖于某个库,并且对该库的调用失败,则该插件最了解如何从该错误中恢复。冒泡该错误并没有帮助,因为 collectd 核心永远不会知道插件 N+1
另一方面,OpenGL 应用程序通常对遇到的任何错误负责,并且可以尝试纠正错误。例如,如果他们试图编译一个着色器,但可能有一个针对特定供应商或平台的特殊着色器。
根据您的程序设计,请考虑以下事项:
- 冒泡错误会让你做出更好的决定吗?
- 您的应用程序是否可以纠正可能出现的错误?
- EG 如果您无法打开套接字或文件,您可以重试或失败,仅此而已。
- 如果您创建一个
GetLastError()
函数,是否存在围绕全局状态的限制?
更新:
使用全局状态选项,您可能会遇到这样的情况:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
char* last_err = NULL;
void set_err(char* error_message) {
if(last_err)
free(last_err);
/* Make a deep copy to be safe.
* The error string might be dynamically allocated by an external library.
* We can't know for sure where it came from.
*/
last_err = strdup(error_message);
}
int can_sqrt(int a) {
if(a < 0) {
set_err("We can't take the square root of a negative number");
return 0;
}
return 1;
}
int main(int argc, char* argv[]) {
int i = 1;
for(i = 1; i < argc; i++) {
int square = atoi(argv[i]);
if(can_sqrt(square)) {
fprintf(stdout, "the square root of %d is: %.0f\n", square, sqrt(square));
} else {
fprintf(stderr, "%s\n", last_err);
}
}
return 0;
}
运行上面的程序
$ ./a.out -1 2 -4 0 -6 4
We can't take the square root of a negative number
the square root of 2 is: 1
We can't take the square root of a negative number
the square root of 0 is: 0
We can't take the square root of a negative number
the square root of 4 is: 2