Error thrown not showing

Hello guys in my template header I defined a company std::vector<T*> employeeContainer

The last exception I'm testing is to check if an employee exists before I remove the said object.

Here's how the function looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
void removeEmployee(T& emp){
    int validate = searchEmployee(emp);
    try {			
        if (validate != -1)
	    employeeContainer.erase(employeeContainer.begin()+validate);
	else
	    throw std::invalid_argument("EMPLOYEE NOT FOUND");
	}
	catch(const std::invalid_argument &error){	
            std::cerr << "\n" << error.what() << employeeContainer[validate]->getEmployeeID();
	    std::cerr << "\n";
	}
}


The search function is defined here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int searchEmployee(T &emp) const{

    int validate = -1;
    for (int i = 0; i < getNumberOfEmployees(); i++){
	if (employeeContainer[i]->getEmployeeName() == emp.getEmployeeName())
     	    if(employeeContainer[i]->getEmployeeID() == emp.getEmployeeID())
		if(employeeContainer[i]->getEmployeePosition() == emp.getEmployeePosition())
		    if (employeeContainer[i]->getEmployeeSeniority() == emp.getEmployeeSeniority()){
			validate = i;
		        break;
		    }
    }
    return validate;	
}


In the driver file I create an employee but purposely don't add it to the employeeContainer. However, when I attempt to remove the non-existent object no exception is thrown. Is this normal? Am I doing something wrong?
The search function is working as intended because it detects if am employee already exists when I'm adding an employee to avoid duplicate.

ALSO: when I attempt to remove an existing employee the function works. It only fails to throw an error/display an error.
Last edited on
What do you get if you print validate before the try block?

By the way, I really don't see the point of throwing an exception there.
Does it print to the screen if you use std::cout instead of std::cerr?
> std::cerr << "\n" << error.what() << employeeContainer[validate]->getEmployeeID();
That line would be reached if `validate' is -1, so you are accessing the vector out of bounds.


Also, `validate' is a bad name for an index.
Last edited on
@booradley60 No it doesn't print using std::cerr or std::cout


@helios Yeah I really don't see the point of exception here either. This is the last requirement in my stupid assignment and this is preventing me from finish... Q_Q
When I print out validate before the try block I get -1
So it's something with the exception not catching maybe?



@ne555
Yeah it gets a bit confusing, I think a more proper term would be... location?

Could it be an issue with my compiler/Eclipse run?

For clarity, I call another function x2 addEmployee(someobject)
1
2
3
4
5
6
7
8
9
10
11
12
13
void addEmployee(T& emp){
    int validate = searchEmployee(emp);
    try {
	if(validate == -1)
	    employeeContainer.push_back(&emp);
	        else
		    throw ExistingEmployee();
	}
	catch(ExistingEmployee &error){
		std::cerr << "\n" << error.what() << employeeContainer[validate]->getEmployeeID() ;
		std::cerr << "\n";
	}
}


And the error shows up exactly as planned:
Employee already exists ID: 1234
Last edited on
You are invoking undefined behaviour because you are accessing an invalid index of your vector (-1)


> Yeah I really don't see the point of exception here either.
Perhaps you misread the assignment, ¿why should removeEmployee() print anything?
The try-catch block may be outside the function, like in main
Last edited on
Topic archived. No new replies allowed.