CWE Rule 755
Description
Rule Description
The product does not handle or incorrectly handles an exceptional condition.
Polyspace Implementation
The rule checker checks for Exception handler hidden by previous handler.
Examples
Exception handler hidden by previous handler
This issue occurs
when a catch
statement is not reached because a
previous catch
statement handles the exception.
For instance, a catch
statement accepts an
object of a class my_exception
and a later catch
statement
accepts one of the following:
An object of the
my_exception
class.An object of a class derived from the
my_exception
class.
Because the catch
statement is not reached,
it is effectively dead code.
One possible fix is to remove the redundant catch
statement.
Another possible fix is to reverse the order of catch
statements.
Place the catch
statement that accepts the derived
class exception before the catch
statement that
accepts the base class exception.
#include <new> extern void print_str(const char* p); extern void throw_exception(); void func() { try { throw_exception(); } catch(std::exception& exc) { print_str(exc.what()); } catch(std::bad_alloc& exc) { //Noncompliant print_str(exc.what()); } }
In this example, the second catch
statement
accepts a std::bad_alloc
object. Because the std::bad_alloc
class
is derived from a std::exception
class, the second catch
statement
is hidden by the previous catch
statement that
accepts a std::exception
object.
The defect appears on the parameter type of the catch
statement.
To find which catch
statement hides the current catch
statement:
On the Source pane, right-click the keyword
catch
and select Search For"catch"
in Current Source File.On the Search pane, click each search result, proceeding backward from the current
catch
statement. Continue until you find thecatch
statement that hides thecatch
statement with the defect.
catch
StatementOne possible correction is to place the catch
statement
with the derived class parameter first.
#include <new> extern void print_str(const char* p); extern void throw_exception(); void corrected_excphandlerhidden() { try { throw_exception(); } catch(std::bad_alloc& exc) { print_str(exc.what()); } catch(std::exception& exc) { print_str(exc.what()); } }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)