Skip to main content

Secondary navigation

  • Downloads
  • Integrations
  • Blog
  • Company
    • About Us
    • Team
    • Culture
    • Careers
    • Partners
    • Press
    • Events
    • Security & Compliance
  • Contact
    • Contact Us
    • Request Support
    • Subscribe
  • Language
    • English
    • 日本語
    • 简体中文
    • 한국어
Home
Perforce

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • Explore Products
    • All Products
    Helix Core
    Version Control
    Helix TeamHub
    Code Hosting for Git, SVN, Hg
    Methodics IPLM
    IP Lifecycle Management
    Gliffy
    Diagramming
    JRebel
    Java Application Development
    Helix DAM
    Digital Asset Management
    Helix Core
    Version Control
    Helix TeamHub
    Code Hosting for Git, SVN, Hg
    Methodics IPLM
    IP Lifecycle Management
    Gliffy
    Diagramming
    JRebel
    Java Application Development
    Helix DAM
    Digital Asset Management
  • Solutions

    Main Navigation - Mega Menu

    • Explore Solutions
    • Solutions Overview

    Main Navigation - Mega Menu

    • By Need
    • By Industry

    Main Navigation - Mega Menu

    • Application Lifecycle Management
    • Agile Project Management
    • Diagramming
    • DevOps
    • Version Control
    • IP Lifecycle Management
    • Java Application Development
    • Web & Mobile App Testing
    • Codeless Selenium Automation
    • Static Analysis & SAST
    • Audit & Compliance
    • API Management
    • Open Source Support
    • Enterprise PHP
    • HPC Debugging
    • Development Tools & Libraries

    Main Navigation - Mega Menu

    • Aerospace & Defense
    • Automotive
    • Embedded Systems
    • Semiconductor
    • Energy
    • Financial
    • Game Development
    • Virtual Production
    • Government
    • Medical Devices
    • Software
    • Digital Twins

    Main Navigation - Mega Menu

    Main Navigation - Mega Menu

    Main Navigation - Mega Menu

    Main Navigation - Mega Menu

  • Customers
  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Papers & Videos
    • Recorded Webinars
    • Events & Webinars
    • Blog
    • Free Trials
    • Subscribe
    Version Control in Virtual Production

    Version Control in Virtual Production Field Guide

    Read Now
  • Support
  • Services

    Main Navigation - Mega Menu

    • Consulting/Professional Services
    • Training

    Main Navigation - Mega Menu

    • Consulting Services Overview
    • Akana
    • BlazeMeter
    • Helix ALM
    • Helix Core
    • Helix QAC
    • Klocwork
    • Methodics IPLM
    • OpenLogic
    • Perfecto
    • Zend

    Main Navigation - Mega Menu

    • Training Overview
    • Hansoft
    • Helix ALM
    • Helix Core
    • Helix QAC
    • Klocwork
    • OpenLogic
    • Perfecto
    • Zend
  • Try Free
  • Downloads
  • Integrations
  • Blog
  • Company

    Main Navigation - Mega Menu

    • About Us
    • Careers
    • Culture
    • Events
    • Partners
    • Press
    • Team
  • Contact

Version 4.0

High Integrity C++ Coding Standard

Released October 3, 2013

Request PDF Version
High Integrity C++ Coding Standard
0. Introduction
1. General
2. Lexical Conventions
3. Basic Concepts
4. Standard Conversions
5. Expressions
6. Statements
7. Declarations
8. Definitions
9. Classes
10. Derived Classes
11. Member Access Control
12. Special Member Functions
13. Overloading
14. Templates
15. Exception Handling
16. Preprocessing
17. Standard Library
18. Concurrency
19. References
20. Revision History
21. Conditions of Use

15. Exception Handling

15.1 Throwing an Exception


15.1.1 Only use instances of std::exception for exceptions

Exceptions pass information up the call stack to a point where error handling can be performed. If an object of class type is thrown, the class type itself serves to document the cause of an exception. Only types that inherit from std::exception, should be thrown.

#include <cstdint>
#include <stdexcept>
#include <iostream>
               
int foo ();
               
void bar ()
{
try
{
if (0 == foo ())
{
throw -1;     // @@- Non-Compliant [email protected]@
}
}
catch (int32_t e) // @@- Non-Compliant [email protected]@
{
}
                 
try
{
if (0 == foo ())
{
throw std::runtime_error ("unexpected condition"); // @@+ Compliant [email protected]@
}
}
catch (std::exception const & e)                       // @@+ Compliant [email protected]@
{
std::cerr << e.what ();
}
}

If an instance of an object inheriting from std::exception is created, then such an object must appear in a throw expression.

#include <exception>
               
class MyException : public std::exception
{
// ...
};
   
void f1 ()
{
MyException myExcp;   // @@- Non-Compliant [email protected]@
}
   
void f2 ()
{
MyException myExcp;   // @@+ Compliant [email protected]@
throw myExcp;
}

References

  • HIC++ v3.3 – 9.2

View references >

15.2 Constructors and Destructors


15.2.1 Do not throw an exception from a destructor

The 2011 C++ language standard states that unless a user provided destructor has an explicit exception specification, one will be added implicitly, matching the one that an implicit destructor for the type would have received.

#include <stdexcept>
class A
{
public:
~A ()        // @@- Non-Compliant: Implicit destructor for A would be declared with [email protected]@
             // @@-   noexcept, therefore this destructor is noexcept [email protected]@
{
throw std::runtime_error ("results in call to std::terminate");
}
};

Furthermore when an exception is thrown, stack unwinding will call the destructors of all objects with automatic storage duration still in scope up to the location where the exception is eventually caught. The program will immediately terminate should another exception be thrown from a destructor of one of these objects.

#include <cstdint>
#include <stdexcept>
               
class A
{
public:
A () : m_p () {}
               
~A () noexcept(false)
{
if (nullptr == m_p)
{
throw std::runtime_error ("null pointer in A"); // @@- Non-Compliant [email protected]@
}
}
                 
private:
int32_t * m_p;
};
               
void foo (int32_t i)
{
if (i < 0)
{
throw std::range_error ("i is negative");
}
}
               
void bar ()
{
try 
{
A a;
                   
foo (-1);
}
catch (std::exception const & e)
{
}
}

References

  • HIC++ v3.3 – 9.1

View references >

15.3 Handling an Exception


15.3.1 Do not access non-static members from a catch handler of constructor/destructor function try block

When a constructor or a destructor has a function try block, accessing a non-static member from an associated exception handler will result in undefined behavior.

#include <cstdint>
               
class C
{
public:
C ();
               
private:
int32_t m_i;
};
               
C::C ()
try : m_i()
{
// constructor body
++m_i; // @@+ Compliant [email protected]@
}
catch (...)
{
--m_i; // @@- Non-Compliant [email protected]@
}

References

  • MISRA C++:2008 – 15-3-3

View references >

15.3.2 Ensure that a program does not result in a call to std::terminate

The path of an exception should be logical and well defined. Throwing an exception that is never subsequently caught, or attempting to rethrow when an exception is not being handled is an indicator of a problem with the design.

bool f1 ();
   
void f2 ()
{
throw;
}
   
void f3 ()
{
try
{
if (f1 ())
{
throw float(0.0);
}
else
{
f2();                // @@- Non-Compliant: No current exception [email protected]@
}
}
catch (...)
{
f2();  
}
}
   
int main ()
{
f3();                // @@- Non-Compliant: If 'float' thrown [email protected]@
}

References

  • MISRA C++:2008 – 15-3-2

View references >

Request PDF Version

 

 

Book traversal links for 15. Exception Handling

  • ‹ 14. Templates
  • High Integrity C++ Coding Standard
  • 16. Preprocessing ›

Footer menu

  • Products
    • Plan
    • Helix ALM
    • Hansoft
    • Create & Develop
    • Helix Core
    • Helix4Git
    • Helix DAM
    • Helix TeamHub
    • Helix Swarm
    • Methodics IPLM
    • VersIC
    • Test & Validate
    • Helix QAC
    • Klocwork
    • Operate, Manage, & Scale
    • SourcePro
    • HostAccess
    • HydraExpress
    • PV-WAVE
    • Stingray
    • Visualization
  • Solutions
    • By need
    • Application Lifecycle Management
    • Agile Project Management
    • DevOps
    • Version Control
    • IP Lifecycle Management
    • Static Analysis
    • Audit & Compliance
    • Backlog Management
    • Project Portfolio Management
    • By industry
    • Aerospace & Defense
    • Automotive
    • Embedded Systems
    • Semiconductor
    • Energy & Utilities
    • Finance
    • Game Development
    • Virtual Production
    • Government
    • Life Sciences
    • Software
  • Services
    • Consulting/Professional Services
    • Consulting Services Overview
    • Akana
    • BlazeMeter
    • Helix ALM
    • Helix Core
    • Helix QAC
    • Klocwork
    • Methodics IPLM
    • OpenLogic
    • Perfecto
    • Zend
    • Training
    • Training Overview
    • Hansoft
    • Helix ALM
    • Helix Core
    • Helix QAC
    • Klocwork
    • OpenLogic
    • Perfecto
    • Zend
  • Resources
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
    • Perforce U
  • Support
  • Customers
    • Case Studies
  • About
    • Our Team
    • Our Culture
    • Careers
    • Press
    • Contact Us
  • Partners
    • Integrations
    • Resellers
  • Quick links
    • Free Trials
    • Subscription Center
    • Customer Support Login
    • Educational Licenses
    • How to Buy
Home
Perforce

Copyright © Perforce Software, Inc. All rights reserved.  |  Sitemap  |  Terms of Use  |  Privacy Policy

Social menu

  • Facebook
  • Twitter
  • LinkedIn
  • YouTube
  • RSS
Send Feedback