Main Content

CERT C++: ERR60-CPP

Exception objects must be nothrow copy constructible

Since R2021a

Description

Rule Definition

Exception objects must be nothrow copy constructible.1

Polyspace Implementation

The rule checker checks for the issue Exception object throws in copy constructor

Examples

expand all

Issue

The issue Exception object throws in copy constructor occurs if any copy constructor of an exception object is able to throw an exception.

Your compiler might elide a copy constructor. Polyspace® does not take compiler optimizations into account and might report violations on elided copy constructors.

Risk

Any exceptions arising from the copy constructor of the exception object becomes an unhandled exception. Such an unhandled exception results in an abrupt termination of the program through a call to std::terminate(), which can result in unexpected behavior.

Fix

To fix this violation, use nothrow copy constructible objects as exception objects. To check if an object is nothrow copy constructible, use the function template std::is_nothrow_copy_contructible. For the syntax of the function, see std::is_nothrow_copy_constructible.

Example — Copy Constructor of Exception Object Can Raise Exception

In this example, the function foo() throws objects of myException class. The class myException contains the string myException::m that contains an error message. Because the copy constructor of myException performs a string operation, the copy constructor can raise an exception and terminate the program abruptly. Polyspace reports a violation of this rule.

#include <exception>
#include <string>
 
class myException : public std::exception { //Noncompliant
  std::string m;
public:
  myException(const char *msg) : m(msg) {} //copy c'tor
   

};
  
void foo() {
  // If some condition doesn't hold...
  throw myException("Condition did not hold");
}
 
void bar() {
  try {
    foo();
  } catch (myException &s) {
    // Handle error
  }
}
Correction — Use nothrow Copy Constructible Class as Exception

To fix this violation, derive myException from std::runtime_error instead of std::exception. The class std::runtime_error handles error messages without throwing any exceptions. Because the copy constructor of myException no longer performs any operations that can raise an exception, the class becomes nothrow copy constructible. Polyspace does not report a violation.

#include <exception>
#include <string>
 
class myException : public std::runtime_error { //Compliant
  
public:
  myException(const char *msg) : std::runtime_error(msg) {} //copy c'tor
   

};
 static_assert(std::is_nothrow_copy_constructible<myException>::value,
              "myException must be nothrow copy constructible");
void foo() {
  // If some condition doesn't hold...
  throw myException("Condition did not hold");
}
 
void bar() {
  try {
    foo();
  } catch (myException &s) {
    // Handle error
  }
}

Check Information

Group: Rule 08. Exceptions and Error Handling (ERR)

Version History

Introduced in R2021a


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.