Well for starters, it would in the original code (corrected to
use appropriate delete operators):
char * my_ptr;
// at this point, my_ptr contains a random value
try
{
my_ptr = new char[987]; // if this new throws...
// other stuff
delete[] my_ptr
}
catch(MyExec &exc)
{
// clean-up
delete[] my_ptr; // this expression is undefined
// error handling goes here
}
Especially in the presence of hard to predict execution paths
uninitialized variables are hard to handle.
In general, it is easier to initialize a variable just once rather
than to assign it and later just change it without having read the
value. After all, the initialization would be wasted. If you attempt
to fold initialization with the assignment, i.e. initialize the
variable with the final value, in the above case with the value
returned from 'new', you will realize that you cannot do so prior to
the try-block (after all, it can throw and you want to handle the
error) nor within the try-block because you need the variable to do
the clean-up. The only viable approach becomes the use of a resource
management class - a good think, IMO.