What happens if an exception is throws from an, object's
constructor and object's destructor?




Answers were Sorted based on User's Feedback



What happens if an exception is throws from an, object's constructor and object's destru..

Answer / dee

if the exception is thrown in constructor, its caught or
function terminate will eb called.
U cannot throw exception from destructor

Is This Answer Correct ?    5 Yes 2 No

What happens if an exception is throws from an, object's constructor and object's destru..

Answer / truong

1. If exception is thrown in the constructor, the destructor will be not called.
2. If exception is thrown in the destructor of a static object, exception will be catch in the main.

Is This Answer Correct ?    3 Yes 0 No

What happens if an exception is throws from an, object's constructor and object's destru..

Answer / som shekhar

When an exception is thrown from the object constructor,
then the destructor of the object wont be called. But to
undone all the things that happened in the c'tor before the
exception is thrown, you can make use of smart
pointer(either boost smart pointer or std auto pointer).

when an exception is thrown from the destructor the compiler
terminates the application.

So it is advisable not to throw an exception in the
destructor , better to do "something else".

Is This Answer Correct ?    2 Yes 0 No




What happens if an exception is throws from an, object's constructor and object's destru..

Answer / akshay

constructor:
the destructors of all the member objects that were
correctly created are called, then code goes to the catch
statement if any

dtors:
legally allowed, however, it should be caught within the
function. if it is not caught, then it may lead to a call to
terminate() function. this call would happen in the case
when the destructor was itself called due to stack unwinding
happening because of another exception in probably some
other function.

Is This Answer Correct ?    2 Yes 2 No

Post New Answer



More C++ General Interview Questions

A milk carton can hold 3.78 litres of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one litre of milk is $0.38, and the profit of each carton of milk is $0.27. Write a C++ program that prompts the user to enter the total amount of milk produced in the morning. Then display the number of milk cartons needed to hold milk, the cost of producing milk, and the profit for producing milk.

2 Answers  


Arrange Doubly linked list in the ascending order of its integral value and replace integer 5 with 7?

0 Answers  


how to swap two strings without using any third variable ?

11 Answers  


What is a constructor initializer list and when we use constructor initializer list?

3 Answers   TCS, Soft Info,


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

0 Answers   CTS,






find the two largest values among the 6 numbers using control structures : do-while,for,if else,nestedif- else ,while. one or two of them.

0 Answers  


What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"?

3 Answers  


Live example for static function?

1 Answers   Infosys,


How to write Multithreaded applications using C++?

2 Answers   TCS, Wipro, Honeywell,


1)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea += sizeof(int); } return 0; } 2)#include <iostream.h> int main() { int *a, *savea, i; savea = a = (int *) malloc(4 * sizeof(int)); for (i=0; i<4; i++) *a++ = 10 * i; for (i=0; i<4; i++) { printf("%d\n", *savea); savea ++; } return 0; } The output of this two programs will be different why?

5 Answers  


Write a corrected statement in c++ so that the statement will work properly. if (4 < x < 11) y=2*x;

0 Answers  


What are the advantages of C++ programming compared to C programming?

2 Answers   HAL,






Categories