Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Corrected the last CS; fixed coding style conformance

...

Code Block
bgColor#FFcccc
langcpp
#include <stdexcept>
 
class S {
  bool shouldThrowshould_throw() const;
 
public:
  ~S() noexcept(false) {
    // Normal processing
    if (shouldThrowshould_throw()) {
      throw std::logic_error("Something bad");
    }
  }
};

...

Code Block
bgColor#FFcccc
langcpp
#include <exception>
#include <stdexcept>
 
class S {
  bool shouldThrowshould_throw() const;
 
public:
  ~S() {
    // Normal processing
    if (shouldThrowshould_throw() && !std::uncaught_exception()) {
      throw std::logic_error("Something bad");
    }
  }
};

...

Code Block
bgColor#FFcccc
langcpp
#include <stdexcept>
 
class SomeClass {
  class Bad {
    bool shouldThrowshould_throw() const;
  public:
    ~Bad() noexcept(false) {
      if (shouldThrowshould_throw()) {
        throw std::logic_error("Something bad");
      }
    }
  };

  Bad bad_member;

public:
  ~SomeClass()
  try {
    // ...
  } catch(...) {
    // Attempt to handle exceptions thrown from Bad destructor.
  }
};

Compliant Code Example (try-block)

...

Code Block
bgColor#FFcccc
langcpp
#include <stdexcept>
 
bool performDeallocperform_dealloc(void *);
 
void operator delete(void *ptr) noexcept(false) {
  if (performDeallocperform_dealloc(ptr)) {
    throw std::logic_error("Something bad");
  }
}

...

Code Block
bgColor#ccccff
langcpp
#include <cstdlib>
#include <stdexcept>
 
bool performDeallocperform_dealloc(void *);
void logFailurelog_failure(const char *);
 
void operator delete(void *ptr) noexcept(falsetrue) {
  if (performDeallocperform_dealloc(ptr)) {
    logFailurelog_failure("Deallocation of pointer failed");
    std::exit(1); // Fail, but still call destructors
  }
}

...