Thread: IQOD #11

Results 1 to 7 of 7
  1. #1
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical

    IQOD #11

    Only 1 response to IQOD #10? Are these getting too hard already?

    Consider the following program:
    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    	public:
    		A() 	{ cout << "A::A()" << endl; }
    		~A() 	{ cout << "A::~A()" << endl; throw "A::exception"; }
    };
    
    
    class B
    {
    	public:
    		B()	{ cout << "B::B()" << endl; throw "B::exception"; }
    		~B() 	{ cout << "B::~B()"; }
    };
    
    int main (int, char**)
    {
    	try
    	{
    		cout << "Entering try...catch block" << endl;
    		A objectA;
    		B objectB;
    		cout << "Exiting try...catch block" << endl;
    	}
    	catch(char *ex)
    	{
    		cout << ex << endl;
    	}
    	return 0;
    }
    What output does it generate and why?
    Last edited by B1ackAnge1; 09-25-2009 at 11:03 AM.

  2. #2
    why06's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    IBM
    Posts
    4,304
    Reputation
    170
    Thanks
    2,203
    My Mood
    Flirty
    Unfortunately I am very much a novice, when it comes to programming.

    "Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."
    - Dwight D. Eisenhower

  3. #3
    lalakijilp's Avatar
    Join Date
    Jan 2008
    Gender
    male
    Posts
    310
    Reputation
    9
    Thanks
    53
    My Mood
    Blah
    i am only at chapter four yet

  4. #4
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    Entering try...catch block
    A::A()
    B::B()
    B::exception
    B::~B()
    A::~A()
    shut down/crash since it only catches 1 exception.


    amiright?
    Ah we-a blaze the fyah, make it bun dem!

  5. #5
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    HD: <!-- Incorrect.. but you're half way there with figuring what's happening -->

  6. #6
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    hmm

    Entering try...catch block
    A::A()
    B::B()
    B::exception
    A::~A()
    A::exception
    B::~B()
    Exiting try...catch block


    if that is incorrect i give up on this one xD I hate exceptions
    Ah we-a blaze the fyah, make it bun dem!

  7. #7
    B1ackAnge1's Avatar
    Join Date
    Aug 2009
    Gender
    male
    Posts
    455
    Reputation
    74
    Thanks
    344
    My Mood
    Cynical
    Noticed I hadn't replied to this one yet...

    What happens is:

    We Enter the Try Block
    "Entering try...catch block"

    A Get's Constructed, output:
    A::A()

    B Get's Construct, output:
    B::B()

    B Now throws an exception which gets caught by the catch handler. However this means that at this point A is going out of scope
    A::~A()

    However now in the destructor of A it throws another exception. Since we're not in a try-catch anymore the program at this point crashes on the exception.

Similar Threads

  1. IQOD #4
    By B1ackAnge1 in forum C++/C Programming
    Replies: 7
    Last Post: 09-20-2009, 01:37 PM
  2. IQOD #2
    By B1ackAnge1 in forum C++/C Programming
    Replies: 37
    Last Post: 09-19-2009, 04:40 PM
  3. IQOD #3
    By B1ackAnge1 in forum C++/C Programming
    Replies: 23
    Last Post: 09-19-2009, 01:15 PM
  4. Extra Credit IQOD #2A
    By B1ackAnge1 in forum C++/C Programming
    Replies: 18
    Last Post: 09-18-2009, 06:39 PM
  5. Interview Question of the Day (IQOD) #1
    By B1ackAnge1 in forum C++/C Programming
    Replies: 26
    Last Post: 09-18-2009, 04:24 PM

Tags for this Thread