I am asking specifically for a C++ implementation. For example, let's say I have the following class:
class Worker{
string worker_name;
string worker_title;
public:
void setWorkerTitle(string title);
}
void Worker::setWorkerTitle(string title){
if(title == "Employee" || title == "Boss"){
worker_title = title;
return;
}
else{
cerr << "Error setting title for " << worker_name << endl;
cerr << title << " is not a valid title." << endl;
exit(1);
}
}
In my main file, I have the following:
Worker worker1(name, title); // Assume the appropriate constructor exists
worker1.setWorkerTitle("This should fail");
Currently, the setWorkerTitle
function will let the programmer know it failed to set the title of the worker named worker_name
. This is useful enough, but is there a way I can add more information to this error to help the programmer find its exact location?
For example, it would be useful to print the line numbers in the program where the error occurred, similar to how a compiler will let you know every line in every file that led up to an error. Another option would be to print the name of the Worker object that caused the failure, not just the worker_name
in the object.