Giriş
Açıklaması şöyle
Şöyle yaparız.
Örnek
std::string'i exception olarak fırlatmak için şöyle yaparız.
Lambda bile exception olarak fırlatılabilir. Şöyle yaparız
Lambda yakalamak için şöyle yaparız.
std::function yakalamak için şöyle yaparız.
Açıklaması şöyle
Create your own exception types when:Kendi exception sınıfımızı std::exception'dan kalıtmak için şöyle yaparız.
1. you might want to differentiate them when handling. If they're different types, you have the option to write different catch clauses. They should still have a common base so you can have common handling when that is appropriate
2. you want to add some specific structured data you can actually use in the handler
std::exception
  EXProjectBase
    EXModuleBase
      EXModule1
      EXModule2
    EXClassBase
      EXClassMemory
      EXClassBadArg
      EXClassDisconnect
      EXClassTooSoon
      EXClassTooLateŞöyle yaparız.
class divide_error : public std::exception {
  const char* what() const {
    return "division by 0";
  }
};
Var Olan Sınıfı Kullanmak
Normalde std::exception'ı kullanmak daha iyi. Şöyle yaparız.void MyClass::function() noexcept(false) {
  // ...
  if (errorCondition) {
    throw std::exception("Error: empty frame");
  }
}void MyClass::function() noexcept(false) {
  // ...
  if (errorCondition) {
    throw EmptyFrame();
  }
}Örnek
std::string'i exception olarak fırlatmak için şöyle yaparız.
try
{
  throw std::string("boom");
}
catch (std::string str)
{
  std::cout << str << std::endl;
}Lambda bile exception olarak fırlatılabilir. Şöyle yaparız
throw "foo";  // throws an instance of const char*
throw 5;      // throws an instance of int
struct {} anon;
throw anon;   // throws an instance of not-named structure
throw []{};   // throws a lambda!Lambda yakalamak için şöyle yaparız.
#include <utility>
auto makeMyLambda(int some_arg)
{
  return [some_arg](int another_arg){ return some_arg + another_arg; };
}
void f()
{
  throw makeMyLambda(42);
}
int main()
{
  try
  {
    f();
  }
  catch (const decltype(makeMyLambda(std::declval<int>()))& l)
  {
    return l(23);
  }
}std::function yakalamak için şöyle yaparız.
try {
  throw std::function<void()>{ []{} }; // Note the explicit conversion
} catch(std::function<void()> const& f) {
  f();
} 
Hiç yorum yok:
Yorum Gönder