Skip to main content

Secondary navigation

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

Main Navigation - Mega Menu

  • Products

    Main Navigation - Mega Menu

    • Explore Products
    • All Products

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale
    Helix Core
    Helix4Git
    Helix TeamHub
    Helix ALM
    Hansoft
    Gliffy
    Perfecto
    TestCraft
    Helix QAC
    Klocwork
    Akana
    OpenLogic
    Zend Server
    TotalView
    XRebel
    HostAccess
    SourcePro
    Stingray
    HydraExpress
    PV-WAVE
    Visualization
    IMSL
    Methodics IPLM
    VersIC
    JRebel
  • Solutions

    Main Navigation - Mega Menu

    • Explore Solutions
    • Solutions Overview

    Main Navigation - Mega Menu

    • By Need
    • By Industry

    Main Navigation - Mega Menu

    • Plan
    • Create & Develop
    • Test & Validate
    • Operate, Manage, & Scale

    Main Navigation - Mega Menu

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

    Main Navigation - Mega Menu

    • Application Lifecycle Management
    • Agile Project Management
    • Diagramming

    Main Navigation - Mega Menu

    • DevOps
    • Version Control
    • IP Lifecycle Management
    • Java Application Development

    Main Navigation - Mega Menu

    • Web & Mobile App Testing
    • Codeless Selenium Automation
    • Static Analysis
    • Audit & Compliance

    Main Navigation - Mega Menu

    • API Management
    • Open Source Support
    • Enterprise PHP
    • HPC Debugging
    • Development Tools & Libraries
  • Customers
  • Resources

    Main Navigation - Mega Menu

    • Explore Resources
    • Papers & Videos
    • Recorded Webinars
    • Events
    • Blog
    • Demos
    • Subscribe
    Image VCS IPreuse Preview new

    How to Make IP Reuse Work For You

    Read Now
  • Support

    Main Navigation - Mega Menu

    • Explore Support
    • Support Plans
    • Self-Service Resources
    • Documentation
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download Software
    • Request Support

    Support

    Get answers quick by searching our public knowledgebase

    Visit Perforce KB

  • 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
    • Version Control System
    • Helix Core
    • Methodics IPLM
    • VersIC
    • Enterprise Agile Planning
    • Hansoft
    • Dev Collaboration
    • Helix TeamHub
    • Helix Swarm
    • Helix4Git
    • Development Lifecycle Management
    • Helix ALM
    • Surround SCM
    • Static Analysis
    • Helix QAC
    • Klocwork
    • Development Tools & Libraries
    • HostAccess
    • HydraExpress
    • PV-WAVE
    • SourcePro
    • Stingray
    • Visualization
  • Solutions
    • By need
    • Version Control
    • Application Lifecycle Management
    • Agile Project Management
    • Backlog Management
    • Project Portfolio Management
    • Audit & Compliance
    • DevOps
    • Static Analysis
    • IP Lifecycle Management
    • By industry
    • Game Development
    • Life Sciences
    • Software
    • Automotive
    • Embedded Systems
    • Government
    • Finance
    • Energy & Utilities
    • Aerospace & Defense
    • Virtual Production
    • Semiconductor
  • Resources
    • Papers & Videos
    • Events & Webinars
    • Recorded Webinars
    • Blog
  • Support
    • Documentation
    • Request Support
    • Video Tutorials
    • Training
    • Consulting
    • Release Notes
    • Download
  • Customers
    • Case Studies
  • About
    • Our Team
    • Our Culture
    • Careers
    • Press
    • Contact
  • Partners
    • Integrations
    • Resellers
  • Quick links
    • Try For Free
    • Helix Core Demo
    • Helix ALM Demo
    • Subscription Center
    • Customer Support Login
    • Licensing Requests
    • Educational Licenses
    • How to Buy
Home
Perforce

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

Social menu

  • Facebook
  • Twitter
  • Google+
  • LinkedIn
  • YouTube
  • RSS