answersLogoWhite

0


Best Answer

We must throw an exception from constructor in C++, but remember that in case of exception in Ctor, destructor will not be executed and you may have memory leaks. SO make sure all the data members must clean up their mess own. e.g. use smart pointers so in case of excpetion memory will released to free storage.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Yes, Constructor can throw an exception in C++. Constructor do not have return type so we cannot use return codes, the best way to report failure in constructor throw an exception. In case of exception in Constructor your object's destructor will not be called and you may get memory leaks. Therefore every data mamber should be responsible to undone their mess. E.g. You may use smart pointers, so will be retuned to free strorage if excpetion occurs in Ctor.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Is constructors throws exception in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How many constructors does class Exception have?

The Exception class has 4 constructors. They are: a. Exception() b. Exception(String arg) c. Exception(String arg, Throwable arg1) d. Exception(Throwable arg)


How exception handling differs in C plus plus and Java?

Exception handling is largely the same for both. The only real difference is that C++ has no 'finally' clause which always executes whether an exception occurs or not. Another difference is that Java throws exceptions and errors, but errors cannot be handled since programs cannot handle errors -- such as the JVM out of memory error.


What is mapping exception in c plus plus?

There is no such exception in C++. It's probably a 3rd party or user-defined exception. Examine the call-stack to determine where the exception was caught -- that should help you determine where the exception was thrown.


What is the difference between throw and throws in net?

"throw" is the keyword to raise an exception. "throws" is a Java keyword that indicates a specific method can potentially raise a named exception. There is no analog in VB.Net or C#. Perhaps there is a "throws" keyword in J#?


How can a constructor be invoked at the time of inheritance in C Plus Plus?

It cannot. Inheritance is a compile-time operation. Constructors are invoked at runtime at the point of instantiation.


Which compiler is used for writing c plus plus exception handling programs?

C++ compiler, obviously, a C compiler won't do.


What are the various concepts of c plus plus?

1.Classes and Objects 2.Constructors and Destructors 3.Inheritance 4.Polymorphism 5.Dynamic Binding


What is exception handling in C plus plus?

Exception handling is the means by which exceptions -- anomalous events -- are resolved without causing a runtime error.


What are different type of constructor in java?

Every class, including abstract classes, MUST have a constructor. The different types are: a. Regular constructors b. Overloaded constructors and c. Private constructors


How many constructors can c have?

A class can have any number of constructors, as far as they are having different parameters or different number of parameters. For example, a class A can have following constructors & even more: A() -the default constructor A(A objectA) -the copy constructor A(int p) A(int p1, int p2) A(int[] p1, float p2) A(double p1, double p2, int p3) A(A objA, int[] p) A(B objB)


What is meant by throw command in c plus plus?

A 'throw' statement throws an exception when it is generated within a try block. The exception is then caught by the corresponding catch block. For example, void somefn() { float m, n; try { cin>>m>>n; if (n == 0) throw(n); else cout<<(m/n); } catch (float x) { cout<<"Division by zero not possible"; } }


What is copy constructor in java?

Java, unlike C++ does not support copy constructors.