0

I would like to be able to say:

typedef std::map<int,std::vecto<uint64_t>> ResRegestry;
typedef std::map<int, ResRegestry> EngagedList;    
EngagedList engaged_list;
//...
try {
    engaged_list.at(some_key).at(another_key));
} catch (EngagedListException& e) {
    cout << "no such key for EngagedList\n";
} catch (ResRegestryException& e) {
    cout << "no such key for ResRegestry\n";
}

Intuitively it seems me that there should be a simple way to set custom exceptions EngagedListException, ResRegestryException without wrapping the whole std::map class. I would appreciate any suggestions. Thanks.

Todder
  • 85
  • 1
  • 6
  • You could make a wrapper class of map , convert map exception to your exceptions.or directly catch map exception and re-throw your exception. – Ron Tang Mar 25 '15 at 08:37
  • read this: http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt why you should encapsulate containers. – Alexander Oh Mar 25 '15 at 08:52

1 Answers1

0

Intuitively it seems me that there should be a simple way to set custom exceptions EngagedListException, ResRegestryException without wrapping the whole std::map class.

There shouldn't be one. The functions of a class define a contract. If the documentation for a function says "this function will throw a std::out_of_bounds in this condition", then that is what the function does.

That said, encapsulating these classes should be easier, when you consider that you will probably not use all methods of the map and vector (so use private encapsulation and just expose what you use in your client code).

utnapistim
  • 26,809
  • 3
  • 46
  • 82