Possible Duplicate:
Error handling in C code
Hello people. I'm using C for some small projects and I see how, because it doesn't have dedicated error handling constructs, I have to pollute my algorithm with extra conditional blocks. My question is how do you prefer to deal with errors, and justification why. I'm torn between two ways... if you have a third one, post it. Thanks.
///////////////////////////////////////////
// method 1
// stuff that can go wrong;
if (test1 == failed)
{
// print error;
// exit;
}
else
{
// more stuff that can go wrong;
if (test2 == failed)
{
// print error;
// exit;
}
else
{
// ... and so on...
}
}
///////////////////////////////////////////
// method 2
// stuff that can go wrong;
if (test1 == failed)
{
// print error;
// exit;
}
// more stuff that can go wrong;
if (test2 == failed)
{
// print error;
// exit;
}
// ... and so on...