Main Content

CERT C++: ERR51-CPP

Handle all exceptions

Description

Rule Definition

Handle all exceptions.1

Polyspace Implementation

The rule checker checks for Unhandled exception not caught.

Examples

expand all

Issue

The checker shows a violation if there is no try/catch in the main function or the catch block does not handle all exceptions by using a catch(...) block. The rule is not checked if a main function does not exist.

The checker does not determine if an exception of an unhandled type actually propagates to main.

Risk

Depending on the hardware and software that you use, unhandled exceptions might result in a call to std::abort(), which aborts program execution without deleting the variables in the stack. Such an abnormal termination results in memory leaks and security vulnerabilities.

Fix

Avoid unhandled exceptions. For instance, execute the operations of main() or task main functions in a try-catch block. In the catch blocks:

  • Handle exceptions of type std::exception explicitly in appropriate catch blocks.

  • Handle the base class of exceptions arising from third-party libraries.

  • Handle unexpected exceptions in a catch(...) block.

Example — Exceptions Might Remain Unhandled
#include <stdexcept>
int main(){   // Noncompliant
  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }
  return 0;
}

In this example, main() handles specific types of exceptions, but lacks a catch(...) block. An unexpected exception remains unhandled. Because main() does not handle all exceptions, Polyspace® raises this defect.

Correction

One possible correction is to include a catch(...) block to handle unexpected exceptions.

#include <stdexcept>
int main(){   // Compliant
  try {
    // program code
  } catch (std::runtime_error& e) {
    // Handle runtime errors
  } catch (std::logic_error& e) {
    // Handle logic errors
  } catch (std::exception& e) {
    // Handle all expected exceptions
  }
  catch(...){
	  //Exit gracefully
  }
  return 0;
}

Check Information

Group: 08. Exceptions and Error Handling (ERR)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.