//throw by value, catch by reference, Item 73
#include <iostream>
#include <string>
using namespace std;
class Exception_just_a_class
{
};
class Exception_store_message
{
public:
Exception_store_message(string message):what(message){}
string get_what() { return what;}
void set_what(const string new_what) { what = new_what;}
private:
string what;
};
void madly_throw_exception()
{
const char BELL = '\a';
try{
throw Exception_just_a_class();
}
catch (Exception_just_a_class){
cout << "catch Exception_just_a_class through type\n";
cout << BELL << endl;
cout << "exception caught, hit enter to throw another exception\n";
try{
string line;
getline(cin, line, '\n');
if ( line.size() == 0)
throw Exception_store_message("throw a second exception");
}
catch ( Exception_store_message &ex){
cout << BELL << endl;
cout << "(trow by value, catch by reference)\n";
cout << ex.get_what() << endl;
ex.set_what("new what");
throw ; //rethrow modified Exception_store_message, that is, ex.
}
}
}
int main()
{
try{
madly_throw_exception();
}
catch (...) {
cout << "I catch everythin before crash!\n" ;
}
}
Search This Blog
Wednesday, June 10, 2009
Throw by Value, Catch by Reference with Beep
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment