c++ - Exceptions with new and exceptions without new operator -
as title says, difference between these 2 ways of throwing exception?
void method1() { //... throw new myexception(); } void method2() { //... throw "my exception"; }
i afraid of memory leaks.
in method1 must free memory allocated new?
in method2, string allocated on heap (again frees it?)? or passed return value on stack?
there many reasons throw value , catch reference
if throw pointer dynamic memory, you'll have manually deal memory management on catch site.
as proposed in c++ coding standards :
if feel must throw pointer, consider throwing value-like smart pointer such
shared_ptr<t>
instead of plaint*
.
Comments
Post a Comment