Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Hand waving

...

Code Block
bgColor#FFcccc
langcpp
#include <stdexcept>
 
class SomeClass {
  class Bad {
    bool has_error() const;
  public:
    ~Bad() noexcept(false) {
      if (has_error()) {
        throw std::logic_error("Something bad");
      }
    }
  }// Assume that this class is provided by a 3rd party and it is not something
// that can be modified by the user.
class Bad {
  ~Bad() noexcept(false);
};
class SomeClass {
  Bad bad_member;

public:
  ~SomeClass()
  try {
    // ...
  } catch(...) {
    // Handle the exception thrown from the Bad destructor.
  }
};

...

Code Block
bgColor#ccccff
langcpp
#include <stdexcept>
 
class SomeClass {
  class Bad {
    bool has_error() const;
  public:
    ~Bad() noexcept(false) {
      if (has_error()) {
        throw std::logic_error("Something bad");
      }
    }
  }// Assume that this class is provided by a 3rd party and it is not something
// that can be modified by the user.
class Bad {
  ~Bad() noexcept(false);
};
 
class SomeClass {
  Bad bad_member;
public:

  ~SomeClass()
  try {
    // ...
  } catch(...) {
    // Catch exceptions thrown from noncompliant destructors of
    // member objects or base class subobjects.

    // NOTE: Flowing off the end of a destructor function-try-block causes
    // the caught exception to be implicitly rethrown, but an explicit
    // return statement will prevent that from happening.
    return;
  }
};

...