Declaration of throw for generic exception
Description
This defect occurs when a function throws a generic exception. Polyspace® checks the throw statements, dynamic exception specifications, or the documentation of a function and raises this defect if any of these conditions is true:
The function contains
throw
statements that raise generic exceptions.The dynamic exception specification contains generic exceptions.
The function is documented with a
@throw
comment that contains generic exceptions.
Polyspace considers exceptions of the class std::exception
as
generic.These are base exception classes that have many derived exception classes.
Risk
Raising generic exceptions hinders proper error handling and error recovery. For instance:
void foo(void) throw(std::exception){ //... } void caller(void){ try{ //... foo(); }catch(std::exception& e){ // Need to figure out what the error is. } }
foo()
, determining what errors might
happen in foo()
is handled by caller()
. Because the
exception specification of foo()
is broad, the emergence of new or
unexpected exceptions might not be detected. Handling and recovering from errors in
foo()
becomes more complicated for caller()
.Fix
To resolve this defect, use specific exceptions when documenting the exception in a function. Caller functions are then able to address specific exceptions by using specific error-handling code. Unexpected exceptions are easily detected.
Examples
Result Information
Group: Good practice |
Language: C++ |
Default: Off |
Command-Line Syntax:
THROW_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)