Possible Duplicate:
Exceptions or error codes
When is it appropriate to use error codes?
I'm wondering when we should use error codes as used in languages such as C and when to use exceptions. Consider the following example in which we need a getData() function which should return a string of data if successful and error messages otherwise. How should we report errors? as exceptions or error messages?
public int fo(arguments) {
//some code
String data = "";
String errMsg = "";
boolean rc = getData(arguments, data, errMsg);
// do something based on the results of getData()
// and report error messages in case of errors
}
What do you think about the following implementation:
boolean getData(some arguments, String data, String errorMessage){
{
//check arguments for null pointers or invalid values and return error
//message
errorMessage = "Invalid Arguments";
return false;
}
{
//check for other errors
errorMessage = "Some Error";
return false;
}
// no error, return valid data
data = "some valid data";
return true;
}
As far as I know we shouldn't use exceptions for flow control. Could you please comment on proper use of error codes and exceptions?