Declaration of catch for generic exception
A catch
block handles a generic exception that might have many
different subtypes
Since R2022a
Description
This defect occurs when you design catch
blocks to handle generic high
level exceptions. Polyspace® checks the exception specifications of a function and raises a violation if a
catch block handles:
All exceptions by using a catch-all block.
Exception of the class
std::exception
. Becausestd::exception
is the base class for all standard C++ exceptions, a catch block that handlesstd::exception
type matches all derived exceptions.
Risk
Using generic catch blocks hides the emergence of unexpected new exceptions and hinders their proper handling. Such generic catch blocks makes the code vulnerable to security issues. For instance:
void foo(void){ try{ //.. }catch(std::exception& e){ //Log error } }
catch
block handles the generic std::exception
class, this code hides unexpected exceptions or those that need to be handled differently.
For instance, If an unexpected std::invalid_argument
exception is raised
in foo()
, it might remain undetected by the developer because it is
matched with the catch block. Because the catch block is not programmed to handle
std::invalid_argument
properly, the poorly handled exception becomes a
vulnerability for the code.Fix
To fix this defect, avoid catching high-level generic exceptions. Write catch blocks that handle specific exceptions to enable handling different exceptions in different ways. Unexpected or new exceptions are also easily detected when catch blocks are specific.
Examples
Result Information
Group: Good practice |
Language: C++ |
Default: Off |
Command-Line Syntax:
CATCH_FOR_GENERIC_EXCEPTION |
Impact: Low |
Version History
Introduced in R2022a
See Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)