I've always read that you should never put code in a constructor that might cause an exception. For example if you need to allocate memory in the contructor then put it in an Initialize() method where the exception can be safely caught and/or thrown.

>I've always read that you should never put code
>in a constructor that might cause an exception.
Perhaps you're thinking of destructors and the whole throwing an exception while processing an exception nightmare. The best way to handle a fatal error in the constructor is to throw. Otherwise you're stuck with a partially constructed object, and that way lies madness.

>For example if you need to allocate memory in the contructor then put it in
>an Initialize() method where the exception can be safely caught and/or thrown.
If you're calling the method from the constructor, you've still got the same problem: dealing with a thrown exception within the constructor call chain. Catching an exception can be done in the constructor as well; adding another level of abstraction doesn't change anything if you catch. In fact, it's a good solution (catching and rethrowing) because you have to clean up your mess before throwing a new exception or rethrowing. Destructors aren't called if the constructor throws.

>I had something like this in mind
That defeats the purpose of a constructor. It's also no better than leaving the object in an uninitialized state if the constructor fails, because you have an "initialized--just kidding" object that's in a limbo state until you call a member function. That kind of interface design is painful to use.

>you have to put the whole class in try/catch which might be undesireable or even not possibe
So you'll penalize all users of the class based on a rare or hypothetical test case?

>In the above obj can not be used outside the try block.
Nor should it, if you're using exception handling properly. Also, keep in mind that thrown exceptions unwind the call stack until a catch is found. So if the try block bothers you that much, you can rely on a catch higher up. For example:

Now you're using obj "outside" a try block, but it's still protected by a catch higher up in the call chain. Or you can even ignore the exception altogether and rely on the default behavior of terminating the program.

Hi. so this is actually a continuation from another question of mine[Here](https://www.daniweb.com/programming/software-development/threads/506795/dynamically-add-values-into-datagridview-cell-from-listbox-vb2010) but i was advised to start a new thread as the original question …

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...