Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langcpp
#include <exception>
#include <string>

class S : public std::exception {
  std::string Mm;
public:
  S(const char *msg) : Mm(msg) {}
  
  const char *what() const noexcept override {
    return Mm.c_str();   
  }
};
 
void g() {
  // If some condition doesn't hold...
  throw S("Condition did not hold");
}

void f() {
  try {
    g();
  } catch (S &s) {
    // Handle error
  }
}

...

Code Block
bgColor#ccccff
langcpp
#include <stdexcept>
#include <type_traits>

class S : public std::exception {
  std::runtime_error Mm;
public:
  S(const char *msg) : Mm(msg) {}
  
  const char *what() const noexcept override {
    return Mm.what();   
  }
};
 
static_assert(std::is_nothrow_copy_constructible<S>::value,
              "S must be nothrow copy constructible");
 
void g() {
  // If some condition doesn't hold...
  throw S("Condition did not hold");
}

void f() {
  try {
    g();
  } catch (S &s) {
    // Handle error
  }
}

...

...