Throw new std :: exception vs throw std :: exception

looking at some code that I came across:

throw /*-->*/new std::exception ("//... 

and I always thought that you do not need / you should not use new here.
What is the correct way, both are fine, if so, is there a difference?

By the way, from what I see, while "grepping" with PowerShell boost libs never uses throw new .

PS I also found CLI code that uses throw gcnew . This is normal?

+83
c ++ exception
Jun 08 2018-12-12T00:
source share
4 answers

The usual way to exclude and exclude exceptions is to throw an exception object and attach it by reference (usually a const reference). C ++ requires the compiler to generate the appropriate code to create an exception object and correctly clear it at the appropriate time.

Throwing a pointer to a dynamically allocated object is never a good idea. Exceptions are supposed to allow you to write more robust code in the face of errors. If you throw an exception object in the usual way, you can be sure that if it is caught using a catch clause that calls the correct type, using catch (...) , whether it is reinserted or not, it will be correctly destroyed at the right time. . (The only exception is if it never comes across, but this is not a recoverable situation if you look at it.)

If you throw a pointer to a dynamically allocated object, you must be sure that no matter what the call stack looks at the point at which you want to throw an exception, there is a catch block that names the correct pointer type and has a corresponding delete call. Your exception should never be caught by catch (...) , unless that block repeats the exception, which is then caught, is another catch block that handles the exception correctly.

Effectively, this means that you used an exception handling function, which should simplify writing reliable code and it is very difficult to write code that is correct in all situations. This leaves aside the problem that it will be almost impossible to act as library code for client code that does not expect this function.

+70
Jun 09 2018-12-12T00:
source share

No need to use new when throwing an exception.

Just write:

 throw yourexception(yourmessage); 

and catch how:

 catch(yourexception const & e) { //your code (probably logging related code) } 

Note that yourexception should be yourexception directly or indirectly from std::exception .

+26
Jun 08 2018-12-12T00:
source share

The throw new std::exception true if the calling site expects catch std::exception* . But no one expects to catch a pointer to an exception. Even if you document what your function does and people read the documentation, they can still forget and try to catch a reference to the std::exception object.

+20
Jun 08 2018-12-12T00:
source share

C ++ Frequently Asked Questions:

In principle, "if there is a good reason not to do this, catch the link." Avoid catching by value, as this causes copying, and the copy may have different behavior from what was thrown away. Only in special circumstances should you catch the pointer. "

+7
Jun 08 2018-12-12T00:
source share



All Articles