General Error handler for whole program

Jun 2, 2010 at 4:33pm
Ok, I am very frustrated. I have done a lot of homework on this subject and I just can not figure it out.
I want to write a header file and .cpp file to catch any runtime error at any time.
I have looked at signal.h and the try catch, but what I want to be able to do is catch any error from any function at run time. The way my program is written makes this essectial in that it loops about 1000 times and will throw an error. I want to be able to catch the error instead of it shutting down my program. Basically I would like to put a try catch around my whole program. I have a bug_log function that records all variable data and what function I am in, but I do not know how to trigger it.
Thanks in advance if possible,
Don
Jun 2, 2010 at 4:37pm
Any time you have a check that has the potential to fail write in an else statement that passes the returned error to the function that you designate to 'catch' it. I do not know what API you are using so unfortunatly I can not help you further until you tell me.
Jun 2, 2010 at 4:51pm
I am using c++ and c in code::blocks if that is what you mean. The api's that I am using is Curl, libmysqlclient and standard c and c++ header files, however I am not sure that I explained myself properly.

I have multiple header files with their .cpp files that I wrote. What I want to be able to do is put something instead main that will catch an error anyplace in the program:
Example:
main (void)
{
// catch all errors
// something needed here to call bug_log
callfunction()
}

void callfunction()
{
callsecondfunction()
}

void callsecondfunction()
{
// trows an unexpected clib or something
}

void bug_log
{
//process error
}
I would like to add that all of this functions are in separate classes and header files. Maybe this is not possible, but it sure would be handy.
Thanks,
Donald
Jun 2, 2010 at 5:00pm
Then you need to have your functions all return to an intermediate function that checks the data and if it is good, sends it back to main() if it is bad sends it to bug_log().

EDIT: Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

main()
{

check(funcname);
//...
}

{
//...
check(funcname)
{
if(funcname==func)
{func();
//check data returned from func()
if(data==good)
{
return data;
}
else
{
bug_log(data);
return 1;
}
}

//repeat for every function
}

func()
{ 
//...
return data;
}


Last edited on Jun 2, 2010 at 5:05pm
Topic archived. No new replies allowed.