Rule Definition
If an exception object of pointer type is thrown and that pointer refers to a dynamically created object, then it may 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.
Remediation
It is recommended that exceptions should always be thrown by value.
Violation Code Sample
class A
{
// Implementation
};
void fn ( int16_t i )
{
static A a1;
A * a2 = new A;
if ( i > 10 )
{
throw ( &a1 ); // Non-compliant – pointer type thrown
}
else
{
throw ( a2 ); // Non-compliant – pointer type thrown
}
}
Fixed Code Sample
class A
{
// Implementation
};
void fn ( int16_t i )
{
static A a1;
if ( i > 10 )
{
throw ( a1 ); // Compliant – thrown by value
}
.
.
.
Reference
MISRA 2008 C++, 15-0-2: An exception object should not have pointer type.
Related Technologies
Technical Criterion
Efficiency - Memory, Network and Disk Space Management
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.