C and C++


LANG.STRUCT.UCTCH : Unreachable Catch

Summary

An exception handler will never be executed because an earlier handler catches all its exceptions.

There are three possible cases.

Properties

Class Name Unreachable Catch
Significance redundancy
Mnemonic LANG.STRUCT.UCTCH
Categories
AUTOSARC++14 AUTOSARC++14:M15-3-6 Where multiple handlers are provided in a single try-catch statement or function-try-block for a derived class and some or all of its bases, the handlers shall be ordered most-derived to base class.
  AUTOSARC++14:M15-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.
MisraC++2008 MisraC++2008:15-3-6 Where multiple handlers are provided in a single try-catch statement or function-try-block for a derived class and some or all of its bases, the handlers shall be ordered most-derived to base class.
  MisraC++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.
CWE CWE:561 Dead Code
  CWE:703 Improper Check or Handling of Exceptional Conditions
CERT-CPP CERT-CPP:ERR51-CPP Handle all exceptions
  CERT-CPP:ERR54-CPP Catch handlers should order their parameter types from most derived to least derived
JSF++ JSF++:186 There shall be no unreachable code.
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Unreachable Catch"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#include <exception>

class derived_excp : public std::exception {};
class more_derived_excp : public derived_excp {};

namespace lang_struct_uctch{

  void most_to_least(void){
    try {
    } catch ( more_derived_excp ) {
    } catch ( derived_excp ) {                 // ok: base class of more_derived_excp 
    } catch ( std::exception ) {               // ok: base class of derived_excp and more_derived_excp 
    }
  }

  void least_to_most(void){
    try {
    } catch ( std::exception ) {
    } catch ( derived_excp ) {      // 'Unreachable Catch' warning issued here
    } catch ( more_derived_excp ) { // 'Unreachable Catch' warning issued here
    }
  }

  void excp_ptr(void){
    try {
    } catch ( std::exception* ) {
    } catch ( derived_excp* ) {     // 'Unreachable Catch' warning issued here
    }
  }

   void excp_ref(void){
    try {
    } catch ( std::exception& ) {
    } catch ( derived_excp& ) {     // 'Unreachable Catch' warning issued here
    }
  }

  void void_ptr(void){
    try {
    } catch ( void*) {
    } catch ( std::exception* ) {   // 'Unreachable Catch' warning issued here
    }
  }

  void ellipsis(void){
    try {
    } catch (...) {
    } catch ( std::exception ) {    // 'Unreachable Catch' warning issued here
    }
  }

}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.