Alan wrote in message ...
[ please do not top-post. ] [re-orderef]
Do you mean I should be placing it around larger blocks of code
rather than isolating single, memory allocation-related statements?
Thanks, Alan
If you are looking for an unknown problem, you could put it in main().
#include <stdexcept>
#include <iostream> // etc.
#include <vector>
class BumFaddle{
void funcy(){
try{
throw std::runtime_error("from funcy");
}
catch( std::runtime_error &Re){
std::cerr<<"BumFaddle caught: "<<Re.what()<<std::endl;
throw; // send it on
}
} // funcy()
};
int main(){
try{
// .... code ....
BumFaddle Bf;
Bf.funcy();
std::vector<int> Vint(2);
int num = Vint.at(3); // out_of_range
} // try
// catch( [1] ){
// }
catch( const std:

ut_of_range &Oor ){
std::cout<<"caught "<<Oor.what()<<std::endl;
}
catch( const std::exception &e ){ // Salt's example
std::cerr << "error: ";
std::cerr << e.what() << std::endl;
}
// if you put "catch( out_of_range )" here, it will never get
// to it due to the 'higher-up' above.
// catch( [1] ){
// }
catch( ... ){ // catch anything not caught above.
std::cout<<"caught something (maybe the flu!!)"<<std::endl;
} // the 3 fots are really 3 dots, not a place holder.
return 0;
}
// --- sample output ---
// BumFaddle caught: from funcy
// error: from funcy // note how 'exception' caught 'runtime_error'
[1] one of (all std:

:
exception
Base class for all the exceptions thrown by C++ Standard library.
You can call what() and retrieve the optional string with which the
exception was initialized.
logic_error [ Derived from exception. ]
Reports program logic errors.
runtime_error [ Derived from exception. ]
Reports runtime errors.
[ Exception classes derived from logic_error ]
domain_error
invalid_argument
length_error
out_of_range
bad_cast
bad_typeid
[ Exception classes derived from runtime_error ]
range_error
Reports violation of a postcondition.
overflow_error
Reports an arithmetic overflow.
bad_alloc
Reports a failure to allocate storage.
// OR almost anything (including class/struct):
try{
if( true ){ throw "Help Me!!";}
}
catch( char const *r_e ){
std::cout<<"error: " << r_e << std::endl;
}