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.
Charles Bailey Jun 09 2018-12-12T00: 00Z
source share