Error Checking -

Hi,

I have a quick question about error checking. If I wish to check the parameters passed to a function, should I do this before calling the function, within the function or both? Should this choice be based on where I wish to catch and process the error? (Please refer to example below.)

Thanks...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void fnc(int param1, int param2) {
	/*
		code for error checking parameters passed to function
	*/
}

int main(int argc, char * agrv[]) {
	int var1, var2;
	/*
		code to assign values to var1 & var2
	*/

	/*
		code for error checking var1 & var2 prior to calling fnc				
	*/

	fnc(var1, var2);

	return 0;
}
.
In general, I would say check for errors in the function. If you don't, you'll end up having duplicate error checking code wherever you use the function. Depending on the situation, you may throw an exception or return a special/default value.
Hi Zhuge,

Yeah, getting tired of writing duplicate error code is what prompted me to post this question.

Thanks for your reply…
Topic archived. No new replies allowed.