Why is this an issue?
The catch-all handler, written catch(...)
in C++, or @catch(...)
in Objective-C, catches every type of exception. If
there is another catch statement for a specific exception after the catch-all handler, it will not be executed because the catch-all handler will
already have handled the exception.
How to fix it
Code examples
The following C++ example is similar in Objective-C: the try
and catch
equivalents are @try
and
@catch
.
Noncompliant code example
void f() {
try {
// ...
} catch (...) {
// Handle all exception types
} catch (std::exception const &e) { // Noncompliant: it will never be called
}
}
Compliant solution
void f() {
try {
// ...
} catch (std::exception const &e) {
// Handle standard exceptions
} catch (...) { // Compliant: handle all other exception types
}
}
Resources
Documentation
External coding guidelines
- 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.