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 plain t* .


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -