hi I'm learning c++ from a book, know im on exception handling, but the book do not explain how to deal with exceptions when an error happens while creating an object, how i deal with this? heres my idea but dont works
//im attaking this problem fine or not? how i do it?Code:class Car{ public: Car(int speed){ cout << "constructor called"; if(speed > 500) throw speed; } //I need to catch the exception and call the destructor //any idea? catch(int spd){ cout << "at " << spd " you'r travelinig too fast"; } ~Car(){ cout << "Destructor called"; } }; int main{ Car toyota(1500); //error because 1500 is too fast, so destructor must be called //here, or in other words object must be destroyed. .... //code continues with object toyota already destroyed return 0; }
//the books says !when u throw an exception from a constructor..
//the destructor is called" but this is not working to me"
thanks for any help , and please excuse my poor english



LinkBack URL
About LinkBacks


