THROW-ptr
In this section:
Synopsis
Throw of exceptions by pointer
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
An exception object of pointer type is thrown and that pointer refers to a dynamically created object. It might thus be unclear which function is responsible for destroying it, and when. This ambiguity does not exist if the object is caught by value or reference. This check is identical to MISRAC++2008-15-0-2, MISRAC++2023-18.1.1.
Coding standards
- CERT ERR09-CPP
Throw anonymous temporaries and catch by reference
- MISRA C++ 2008 15-0-2
(Advisory) An exception object should not have pointer type.
- MISRA C++ 2023 18.1.1
(Required) An exception object shall not have pointer type
Code examples
The following code example fails the check and will give a warning:
class Except {};
Except *new_except();
void example(void)
{
throw new Except();
}
The following code example passes the check and will not give a warning about this issue:
class Except {};
void example(void)
{
throw Except();
}