#include #include #include #include using namespace std; class Dans_Exception: public logic_error { public: Dans_Exception(string w) : logic_error(w) { try { msg = HEADER + w; } catch (const length_error & e) { msg = HEADER; } catch (const bad_alloc & e) { msg = HEADER; } } const char * what() const noexcept override { return msg.c_str(); } private: const string HEADER{"Dan's Exception:"}; string msg; }; int main() { // the following is example code only // // DO NOT THROW AN EXCEPTION IN A TRY BLOCK // THAT YOU EXPECT TO CATCH IN THE // CORRESPONDING CATCH BLOCK. // // the following is example code only cout << "Catching as a Dans_Exception" << endl; try { throw Dans_Exception(" customize!"); } catch (const Dans_Exception & e) { cout << " Caught " << e.what() << endl; } catch (const logic_error & e) { cout << " Caught " << e.what() << endl; } catch (const exception & e) { cout << " Caught " << e.what() << endl; } cout << endl; cout << "Catching as a logic_error" << endl; try { throw Dans_Exception(" customize!"); } catch (const logic_error & e) { cout << " Caught " << e.what() << endl; } cout << endl; cout << "Catching as an exception" << endl; try { throw Dans_Exception(" customize!"); } catch (const exception & e) { cout << " Caught " << e.what() << endl; } return 0; }