Table of Contents

std::nested_exception

利用用途

使い方

#include <exception>
#include <iostream>
#include <stdexcept>
#include <string>
 
void g() { throw std::runtime_error {"なんかやる気出ない"}; }
void f()
  try { g(); }
  catch (...) { std::throw_with_nested(std::runtime_error {"gのせいで失敗"}); }
 
void print(const std::exception& e)
try {
  std::cout << e.what() << std::endl;
  std::rethrow_if_nested(e);
} catch (const std::exception& e2) {
  print(e2);
}
 
int main()
try {
  f();
  return 0;
} catch (const std::exception& e) {
  print(e);
  return 1;
}

wandbox