c++11 - C++ nested method try catch (practices) -


i'm new c++ (coming c) , writing class b member of class a.

the member b throw exception if parent tries access data not exist b's container (i throw invalid_argument exception). member b has object can thought of stl container, example.

my question is: if catch original invalid_parameter exception in a, considered c++ practice re-throw same exception (i'd re-throw original error, logging in it, what() different), or can throw std::exception. code has many

try { a.calls_b_exception } catch (std::invalid_argument& ie) {...} catch (std::exception& e) {...}

blocks , wondering if reduce them

try { a.calls_b_exception } catch (std::exception& e) {...}

and still following c++ paradigms. example code similar situation:

class b {     std::vector<int> stuff;      int get_value(int index)     {        if (index >= stuff.size()) {             stringstream err;             err << "--> invalid index " << index << " stuff";             throw std::invalid_argument(err.str());         }     } }  class {     b b;     // assume there methods add bunch of stuff b     int get_value(int index)      {         try {             b.get_value();         } catch (std::invalid_argument& ie) {             stringstream err;             err << ie.what() << "\n\t--> a::get_value(" << index << ")";             // should throw same exception here or considered c++ etiquette?             throw std::exception(err.str());         } catch (std::exception& e) {             throw;    // not anticipating handling happened         }     } }  int main {     a;     // assume more stuff add     try {         a.get_value(-1);     }     catch (std::exception& e) {         stringstream err;         err << "--> invalid index " << index << " main";         throw std::exception(err.str());exception here?     } } 

you cannot instantiate std::exception(std::string), initial approach not work. of took time read through question , me out.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -