If you do not find the element, this is not a condition for an error in your program, you should not return the link (since links cannot be empty). Rather, you should return a pointer (not owning, most likely) and return nullptr if the item was not found:
Item* itemAt(int row, int col) const { try { return _items.at(make_pair(row, col)); } catch(out_of_range& e) { return nullptr; } }
On the other hand, if the search for an element is not an error, then you can return the link (when the element is found) and allow the exception to be thrown when the element is not found - the responsibility for processing it will belong to that part of your code that has strategic knowledge of how handle it:
Item& itemAt(int row, int col) const { return *_items.at(make_pair(row, col)); }
Andy prowl
source share