在C和C++语言中,exit()代表程序的终止。
然而… … 这个exit()却是可以拦截的!这在别人的代码里面调用了exit(),而又无法修改别人的代码时非常有用。来看这一段代码:
#include <stdio.h>
#include <stdlib.h>
int allow;
void intercept(void)
{
if (!allow) {
allow = !allow;
throw 0;
}
}
void save()
{
printf("Emergence save done!\n");
}
int main()
{
atexit(intercept);
try {
exit(1);
}
catch(...) {
save();
}
return 0;
}