I am trying to do a try and catch, but I keep on getting an error for some reason. The error I am getting is the Debug Assertion Failed. I am trying to loop through all the elements of the vector and if I find the element, I return it. Or when I reach the last element and it is not there, then it should do the try and catch but for some reason, I am getting that Debug Assertion Failed. I am also not quite sure if this is the correct way to do it either, but help is much appreciated
Inventory.h
1 2 3 4 5 6 7 8 9 10
Item& Inventory::getEntry(const string& name)
{
int count = 0;
while (_inventory[count].getName() != name && count != _inventory.size())
{
++count;
}
return _inventory[count];
}
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// test copy constructor
Inventory inventory2 = inventory;
testAnswer("Inventory copy constructor 1", inventory2.getEntry("Apples").getUnitPrice(), 1.99f);
inventory.addEntry("Milk", 3000, 3.49f, false);
inventory2.addEntry("Eggs", 4000, 4.99f, false);
// Expect the following to fail
string nameOfTest = "Inventory copy constructor 2";
try {
testAnswerEpsilon(nameOfTest, inventory.getEntry("Eggs").getUnitPrice(), 1.49f);
cout << "FAILED " << nameOfTest << ": expected to recieve an error but didn't" << endl;
}
catch (const exception& e) {
cout << "PASSED " << nameOfTest << ": expected and received error " << e.what() << endl;
}
If name is not found, you return an out of bounds reference at line 9.
Consider two items in the vector:
First time through the loop, count is 0 and increments to 1.
Second time through the loop, count is 1 and increments to 2.
Returning _inventory[2] is an out of bounds reference.
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
struct inventory {
std::string get_item(std::string const &name) {
// If we find the item, return it...
for (auto &&elt : _inventory)
if (elt == name)
return elt;
// ...but if we didn't find it, throw an exception.
// "throw" is how we tell the caller that
// a. this problem can't be handled here;
// b. maybe you can deal with it.
// If nobody can deal with ("handle") this issue, then std::terminate
// gets called, and the program crashes.
throw std::runtime_error{"not found"};
}
std::vector< std::string > _inventory;
};
int main() {
inventory inv;
try {
// If an exception is thrown (and not handled) by any of the code inside
// this try-block, try to handle it below.
inv.get_item("this item does not exist");
} catch (std::exception const &e) {
// If the exception object is a std::exception, execute this handler.
// std::runtime_error is-a kind of std::exception, and it is handled here.
std::cerr << e.what() << '\n';
}
}
Really std::exception is too broad of a category to catch. For example, this program will catch the std::bad_alloc exception that comes from new if memory can't be allocated, but you probably can't do anything about that: you're only interested in whether or not the item wasn't found or whatever.
Real programs might throw std::system_error with their own error codes.