Use vec.at(i).at(j) and must use this in try-catch block since at() will throw std::out_of_range exception if the index is invalid:
try
{
T & item = vec.at(i).at(j);
}
catch(const std::out_of_range & e)
{
std::cout << "either index i or j is out of range" << std::endl;
}
EDIT:
As you said in the comment:
I actually want the program to stop in case the exception is raised. – jbssm 5 mins ago
In that case, you can rethrow in the catch block after printing the message that it went out of range, so that you can know the reason why it stopped. And here is how you rethrow:
catch(const std::out_of_range & e)
{
std::cout << "either index i or j is out of range" << std::endl;
throw; //it rethrows the excetion
}
[...]by.at(...). How else should it work?