Why is this an issue?
The catch-all handler should come last in a chain of catch
or @catch
statements because it catches everything, and any
more-specific catch
/@catch
that comes after it will never be used, even when the relevant condition occurs.
This C++ code sample is very similar to the Objective-C equivalent with @try
and @catch
.
Noncompliant code example
void f1()
{
try
{
// ...
}
catch (...)
{
// Handle all exception types
}
catch (std::exception const &e) // Noncompliant - handler will never be called
{
}
}
Compliant solution
void f1()
{
try
{
// ...
}
catch (std::exception const &e) // Compliant
{
// Handle standard exceptions
}
catch (...) // Compliant catch-all handler
{
// Handle all other exception types
}
}
Resources
- MISRA C++:2008, 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.