KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Possible Duplicate: When is a function try block useful? Difference between try-catch syntax for function This code throws an int exception while constructing the Dog object inside class UseResources . The int exception is caught by a normal try-catch block and the code outputs : Cat() Dog() ~Cat() Inside handler #include <iostream> using namespace std; class Cat { public: Cat() { cout << "Cat()" << endl; } ~Cat() { cout << "~Cat()" << endl; } }; class Dog { public: Dog() { cout << "Dog()" << endl; throw 1; } ~Dog() { cout << "~Dog()" << endl; } }; class UseResources { class Cat cat; class Dog dog; public: UseResources() : cat(), dog() { cout << "UseResources()" << endl; } ~UseResources() { cout << "~UseResources()" << endl; } }; int main() { try { UseResources ur; } catch( int ) { cout << "Inside handler" << endl; } } Now, if we replace the definition of the UseResources() constructor, with one that uses a function try block , as below, UseResources() try : cat(), dog() { cout << "UseResources()" << endl; } catch(int) {} the output is the same Cat() Dog() ~Cat() Inside handler i.e., with exactly the same final result. What is then, the purpose of a function try block ?
Tags (comma-separated)
Save Edits
Cancel