Rule Definition
If the catch-all handler is found before any other handler, that behavior will be performed. Any handlers after the catch-all are unreachable code and can never be executed.
Remediation
ensure that Catch-all occurs last in the sequence of handlers
Violation Code Sample
void f2 ( )
{
try
{
// ...
}
catch( ... ) // catch-all handler
{
// Handle all exception types
}
catch ( int32_t i ) // Non-compliant – handler will never be called
{
}
}
Fixed Code Sample
void f1 ( )
{
try
{
// ...
}
catch ( int32_t i ) // int handler
{
// Handle int exceptions
}
catch( ... ) // catch-all handler
{
// Handle all other exception types
}
}
Reference
MISRA C++ 2008, Rule 15–3–7: Where multiple handlers are provided in a single try-catch statement or function-try-block, any ellipsis (catch-all) handler shall occur last.
AUTOSAR C++ 2014, M15-3-7: Where multiple handlers are provided in a single try-catch statement or function-try-block, any ellipsis (catch-all) handler shall occur last.
Related Technologies
Technical Criterion
CWE-561 - Dead Code
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.